· 9 years ago · Aug 15, 2017, 03:18 PM
1// Role model
2
3@Entity
4@Table(name = "role")
5class Role {
6
7 @Id
8 @Column(name = "id", unique = true)
9 @GeneratedValue(strategy = GenerationType.IDENTITY)
10 open var id: Long? = null
11
12 @Column(name = "name", nullable = false)
13 lateinit var name: String
14}
15
16// Application
17
18interface RoleRepository: CrudRepository<Role, Long>
19
20@SpringBootApplication
21open class Application {
22 @Autowired
23 lateinit var roleRepository: RoleRepository
24
25 @Bean
26 open fun demo(): CommandLineRunner = CommandLineRunner {
27 createRole(roleRepository)
28 print("Roles count: " + roleRepository.count() + "\n")
29 }
30}
31
32fun main(args: Array<String>) {
33 SpringApplication.run(Application::class.java, *args)
34}
35
36fun createRole(roleRepository: RoleRepository) {
37 val r = Role()
38 r.name = "test"
39
40 roleRepository.save(r)
41}
42
43// Bean Config
44
45@Configuration
46@EnableTransactionManagement
47open class BeanConfig {
48
49 @Autowired
50 lateinit open var entityManagerFactory: EntityManagerFactory
51
52 @Bean
53 open fun getSessionFactory() = entityManagerFactory.unwrap(SessionFactory::class.java)
54 ?: throw NullPointerException("factory is not a hibernate factory")
55
56 @Bean
57 @Autowired
58 open fun transactionManager(sessionFactory: SessionFactory) = HibernateTransactionManager(sessionFactory)
59
60}
61
62
63// Output
64
65 :: Spring Boot :: (v1.5.6.RELEASE)
66
672017-08-15 16:32:58.834 INFO 96888 --- [ main] r.t.spring.jpa.versioning.ApplicationKt : Starting ApplicationKt on MBP-Ivan with PID 96888 (/Users/ivansmolin/Projects/spring-jpa-versioning/out/production/classes started by ivansmolin in /Users/ivansmolin/Projects/spring-jpa-versioning)
682017-08-15 16:32:58.839 INFO 96888 --- [ main] r.t.spring.jpa.versioning.ApplicationKt : No active profile set, falling back to default profiles: default
692017-08-15 16:32:58.927 INFO 96888 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@38364841: startup date [Tue Aug 15 16:32:58 MSK 2017]; root of context hierarchy
70Tue Aug 15 16:33:00 MSK 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
71Tue Aug 15 16:33:00 MSK 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
72Tue Aug 15 16:33:00 MSK 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
73Tue Aug 15 16:33:00 MSK 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
74Tue Aug 15 16:33:00 MSK 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
75Tue Aug 15 16:33:00 MSK 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
76Tue Aug 15 16:33:00 MSK 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
77Tue Aug 15 16:33:00 MSK 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
78Tue Aug 15 16:33:00 MSK 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
79Tue Aug 15 16:33:00 MSK 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
802017-08-15 16:33:00.530 INFO 96888 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
812017-08-15 16:33:00.547 INFO 96888 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
82 name: default
83 ...]
842017-08-15 16:33:00.608 INFO 96888 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.0.12.Final}
852017-08-15 16:33:00.609 INFO 96888 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
862017-08-15 16:33:00.610 INFO 96888 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
872017-08-15 16:33:00.664 INFO 96888 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
882017-08-15 16:33:00.786 INFO 96888 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL57InnoDBDialect
892017-08-15 16:33:01.190 INFO 96888 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export
902017-08-15 16:33:01.198 DEBUG 96888 --- [ main] org.hibernate.SQL : drop table if exists role
91Hibernate: drop table if exists role
922017-08-15 16:33:01.205 DEBUG 96888 --- [ main] org.hibernate.SQL : create table role (id bigint not null auto_increment, name varchar(255) not null, primary key (id)) ENGINE=InnoDB
93Hibernate: create table role (id bigint not null auto_increment, name varchar(255) not null, primary key (id)) ENGINE=InnoDB
942017-08-15 16:33:01.230 INFO 96888 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export complete
952017-08-15 16:33:01.265 INFO 96888 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
962017-08-15 16:33:01.504 INFO 96888 --- [ main] o.s.o.h.HibernateTransactionManager : Using DataSource [org.apache.tomcat.jdbc.pool.DataSource@517566b{ConnectionPool[defaultAutoCommit=null; defaultReadOnly=null; defaultTransactionIsolation=-1; defaultCatalog=null; driverClassName=com.mysql.cj.jdbc.Driver; maxActive=100; maxIdle=100; minIdle=10; initialSize=10; maxWait=30000; testOnBorrow=true; testOnReturn=false; timeBetweenEvictionRunsMillis=5000; numTestsPerEvictionRun=0; minEvictableIdleTimeMillis=60000; testWhileIdle=false; testOnConnect=false; password=********; url=jdbc:mysql://localhost:3306/test_calc?serverTimezone=UTC; username=root; validationQuery=SELECT 1; validationQueryTimeout=-1; validatorClassName=null; validationInterval=3000; accessToUnderlyingConnectionAllowed=true; removeAbandoned=false; removeAbandonedTimeout=60; logAbandoned=false; connectionProperties=null; initSQL=null; jdbcInterceptors=null; jmxEnabled=true; fairQueue=true; useEquals=true; abandonWhenPercentageFull=0; maxAge=0; useLock=false; dataSource=null; dataSourceJNDI=null; suspectTimeout=0; alternateUsernameAllowed=false; commitOnReturn=false; rollbackOnReturn=false; useDisposableConnectionFacade=true; logValidationErrors=false; propagateInterruptState=false; ignoreExceptionOnPreLoad=false; useStatementFacade=true; }] of Hibernate SessionFactory for HibernateTransactionManager
972017-08-15 16:33:01.699 INFO 96888 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
982017-08-15 16:33:01.768 INFO 96888 --- [ main] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
992017-08-15 16:33:01.977 DEBUG 96888 --- [ main] org.hibernate.SQL : select count(*) as col_0_0_ from role role0_
100Hibernate: select count(*) as col_0_0_ from role role0_
101Roles count: 0
1022017-08-15 16:33:02.032 INFO 96888 --- [ main] r.t.spring.jpa.versioning.ApplicationKt : Started ApplicationKt in 3.573 seconds (JVM running for 3.999)
1032017-08-15 16:33:02.033 INFO 96888 --- [ Thread-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@38364841: startup date [Tue Aug 15 16:32:58 MSK 2017]; root of context hierarchy
1042017-08-15 16:33:02.035 INFO 96888 --- [ Thread-3] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
1052017-08-15 16:33:02.037 INFO 96888 --- [ Thread-3] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
1062017-08-15 16:33:02.038 WARN 96888 --- [ Thread-3] o.s.b.f.support.DisposableBeanAdapter : Invocation of destroy method failed on bean with name 'entityManagerFactory': java.lang.IllegalStateException: EntityManagerFactory is closed
107
108Process finished with exit code 0