문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
|
java:hibernate:test [2016/09/18 16:06] kwon37xi |
java:hibernate:test [2016/09/24 15:11] (현재) kwon37xi |
||
|---|---|---|---|
| 줄 27: | 줄 27: | ||
| </ | </ | ||
| - | ===== Hibernate 4.2 미만 Spock Spec ===== | + | ===== Hibernate 4.2 미만 Spock Spec 기본 뼈대 |
| <code groovy> | <code groovy> | ||
| /** | /** | ||
| * Hibernate/ | * Hibernate/ | ||
| */ | */ | ||
| - | abstract class AbstractUserTypeSpec | + | abstract class AbstractHibernateSessionSpec |
| - | Configuration configuration; | + | Configuration configuration |
| - | SessionFactory sf; | + | SessionFactory sf |
| + | |||
| + | Session session | ||
| void setup() { | void setup() { | ||
| configuration = new Configuration(); | configuration = new Configuration(); | ||
| + | configuration.addAnnotatedClass(Article) | ||
| - | | + | configuration.setProperty(" |
| - | + | configuration.setProperty(" | |
| - | | + | configuration.setProperty(" |
| - | configuration.setProperty(" | + | configuration.setProperty(" |
| - | configuration.setProperty(" | + | configuration.setProperty(" |
| - | configuration.setProperty(" | + | configuration.setProperty(" |
| - | configuration.setProperty(" | + | configuration.setProperty(" |
| - | configuration.setProperty(" | + | |
| - | configuration.setProperty(" | + | |
| - | configuration.setProperty(" | + | |
| // hibernate 4.2.x 미만 의 ServiceRegistryBuilder 설정. | // hibernate 4.2.x 미만 의 ServiceRegistryBuilder 설정. | ||
| 줄 59: | 줄 59: | ||
| sf = configuration.buildSessionFactory(serviceRegistry); | sf = configuration.buildSessionFactory(serviceRegistry); | ||
| + | |||
| + | session = sf.openSession() | ||
| } | } | ||
| void cleanup() { | void cleanup() { | ||
| + | session.close() | ||
| sf.close() | sf.close() | ||
| } | } | ||
| } | } | ||
| + | |||
| + | // 테스트시 직접 Connection을 맺어 쿼리를 실행하고 검사하고 싶을 경우 | ||
| + | session.doWork({ Connection con -> | ||
| + | groovy.sql.Sql sql = new groovy.sql.Sql(con) | ||
| + | // work with Sql object | ||
| + | } as Work) | ||
| </ | </ | ||
| + | |||
| + | ===== Hibernate 5.2 / Java 8 Test with lambda ===== | ||
| + | * [[http:// | ||
| + | * '' | ||
| + | |||
| + | <code java> | ||
| + | import static org.hibernate.testing.transaction.TransactionUtil.*; | ||
| + | |||
| + | doInJPA( this:: | ||
| + | entityManager.persist( item ); | ||
| + | assertTrue( entityManager.contains( item ) ); | ||
| + | } ); | ||
| + | |||
| + | // or | ||
| + | doInHibernate( this:: | ||
| + | session.persist( item ); | ||
| + | assertTrue( session.contains( item ) ); | ||
| + | } ); | ||
| + | </ | ||
| + | |||