· 9 years ago · Oct 23, 2016, 10:34 AM
1CREATE TABLE IF NOT EXISTS users
2(
3 id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
4 login VARCHAR(45) NOT NULL,
5 password VARCHAR(45) NOT NULL,
6 full_name VARCHAR(100) NOT NULL
7);
8CREATE UNIQUE INDEX users_unique_login_idx ON users (login);
9
10CREATE TABLE IF NOT EXISTS user_roles
11(
12 user_id INTEGER NOT NULL,
13 role VARCHAR(45),
14 CONSTRAINT user_roles_idx UNIQUE (user_id, role),
15 FOREIGN KEY (user_id) REFERENCES users (id)
16);
17
18CREATE TABLE IF NOT EXISTS contacts (
19 id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
20 user_id INTEGER NOT NULL,
21 first_name VARCHAR(45) NOT NULL,
22 last_name VARCHAR(45) NOT NULL,
23 patronymic VARCHAR(45) NOT NULL,
24 mobile_phone_number VARCHAR(15),
25 home_phone_number VARCHAR(15),
26 address VARCHAR(45),
27 email VARCHAR(30),
28 FOREIGN KEY (user_id) REFERENCES users (id)
29);
30CREATE UNIQUE INDEX contacts_unique_idx ON contacts (user_id);
31
32#You can use MySQL DB with next properties
33spring.jpa.database=MYSQL
34spring.datasource.url=jdbc:mysql://localhost:3306/lardi
35spring.datasource.username=root
36spring.datasource.password=2940063
37spring.datasource.driver-class-name=com.mysql.jdbc.Driver
38use.SSL=false
39
40<?xml version='1.0' encoding='utf-8'?>
41<!DOCTYPE hibernate-configuration PUBLIC
42 "-//Hibernate/Hibernate Configuration DTD//EN"
43 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
44
45<hibernate-configuration>
46 <session-factory>
47
48 <!-- properties -->
49 <property name="connection.url">jdbc:mysql://localhost:3306/lardi</property>
50 <property name="connection.username">root</property>
51 <property name="connection.password">2940063</property>
52 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
53 <property name="show_sql">true</property>
54 <property name="format_sql">true</property>
55 <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
56
57
58 <!-- mapping files -->
59 <mapping class="com.model.BaseEntity"/>
60 <mapping class="com.model.NamedEntity"/>
61 <mapping class="com.model.Contact"/>
62 <mapping class="com.model.User"/>
63 <mapping class="com.model.Role"/>
64
65 </session-factory>
66
67</hibernate-configuration>
68
69@Configuration
70@ComponentScan
71@EnableAutoConfiguration
72public class SpringBootWebApplication {
73 public static void main(String[] args) {
74 SpringApplication.run(SpringBootWebApplication.class, args);
75 }
76
77}
78
79@Configuration
80@EnableTransactionManagement
81public class JPAConfig {
82
83 @Bean
84 public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
85 LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
86 em.setDataSource(dataSource());
87 em.setPackagesToScan("com.model");
88
89 JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
90 em.setJpaVendorAdapter(vendorAdapter);
91 em.setJpaProperties(additionalProperties());
92
93 return em;
94 }
95
96 @Bean
97 public DataSource dataSource(){
98 DriverManagerDataSource dataSource = new DriverManagerDataSource();
99 dataSource.setDriverClassName("com.mysql.jdbc.Driver");
100 dataSource.setUrl("jdbc:mysql://localhost:3306/lardi");
101 dataSource.setUsername( "root" );
102 dataSource.setPassword( "2940063" );
103 return dataSource;
104 }
105
106 @Bean
107 public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
108 JpaTransactionManager transactionManager = new JpaTransactionManager();
109 transactionManager.setEntityManagerFactory(emf);
110
111 return transactionManager;
112 }
113
114 @Bean
115 public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
116 return new PersistenceExceptionTranslationPostProcessor();
117 }
118
119 Properties additionalProperties() {
120 Properties properties = new Properties();
121 properties.setProperty("hibernate.hbm2ddl.auto", "validate");
122 properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
123 return properties;
124 }
125}