package com.art.model;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the address database table.
*
*/
@Entity
@NamedQuery(name="Address.findAll", query="SELECT a FROM Address a")
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private int addressid;
private String address;
private String state;
private String zipcode;
//bi-directional many-to-one association to User
@ManyToOne
@JoinColumn(name="userid")
private User user;
public Address() {
}
public int getAddressid() {
return this.addressid;
}
public void setAddressid(int addressid) {
this.addressid = addressid;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
public String getZipcode() {
return this.zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
}
package com.art.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.art.model.Address;
@Repository
public interface AddressRepository extends CrudRepository<Address, Integer>{
}
package com.art.app;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import com.art.config.JPAConfig;
import com.art.model.Address;
import com.art.model.User;
import com.art.repository.AddressRepository;
import com.art.repository.UserRepository;
@SpringBootApplication
@Import(JPAConfig.class)
public class Application {
@Autowired
public AddressRepository addressRepo;
@Autowired
public UserRepository userRepo;
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
}
@Bean
public CommandLineRunner demo(){
System.out.println("Starting Demo");
return (args) -> {
User user = userRepo.findOne(1);
Address userAddress = user.getAddresses().get(0);
System.out.println( user.getEmail() );
System.out.println( user.getUsername() );
System.out.println( userAddress.getAddress() );
};
}
}
package com.art.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import com.art.model.Address;
import com.art.repository.AddressRepository;
import com.art.repository.UserRepository;
@EnableJpaRepositories(basePackages = "com.art.repository")
@Configuration
public class JPAConfig {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[]{"com.art.model"});
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/art?useSSL=false");
dataSource.setUsername("art");
dataSource.setPassword("Clock124");
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Bean
public HibernateJpaSessionFactoryBean sessionFactoryBean() {
HibernateJpaSessionFactoryBean factoryBean = new HibernateJpaSessionFactoryBean();
factoryBean.setEntityManagerFactory(entityManagerFactory().getNativeEntityManagerFactory());
return factoryBean;
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "validate");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
return properties;
}
}
package com.art.model;
import java.io.Serializable;
import javax.persistence.*;
import java.util.List;
/**
* The persistent class for the users database table.
*
*/
@Entity
@Table(name="users")
@NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private int userid;
private String email;
private String username;
//bi-directional many-to-one association to Address
@OneToMany(fetch=FetchType.EAGER, mappedBy="user")
private List<Address> addresses;
public User() {
}
public int getUserid() {
return this.userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public List<Address> getAddresses() {
return this.addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
public Address addAddress(Address address) {
getAddresses().add(address);
address.setUser(this);
return address;
}
public Address removeAddress(Address address) {
getAddresses().remove(address);
address.setUser(null);
return address;
}
}
package com.art.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.art.model.User;
@Repository
public interface UserRepository extends CrudRepository<User, Integer>{
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'jpa-example'
version = '0.1.0'
}
repositories {
mavenCentral()
maven { url "https://repository.jboss.org/nexus/content/repositories/releases" }
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("com.h2database:h2")
runtime('mysql:mysql-connector-java')
testCompile("junit:junit")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}