1
//$Id: ConfigurationTest.java 14736 2008-06-04 14:23:42Z hardy.ferentschik $
2
package org.hibernate.test.annotations;
4
import org.hibernate.HibernateException;
5
import org.hibernate.MappingException;
6
import org.hibernate.Query;
7
import org.hibernate.Session;
8
import org.hibernate.SessionFactory;
9
import org.hibernate.Transaction;
10
import org.hibernate.cfg.AnnotationConfiguration;
11
import org.hibernate.cfg.Configuration;
12
import org.hibernate.cfg.Environment;
15
* @author Emmanuel Bernard
17
public class ConfigurationTest extends junit.framework.TestCase {
18
public void testDeclarativeMix() throws Exception {
19
AnnotationConfiguration cfg = new AnnotationConfiguration();
20
cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
21
cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
22
SessionFactory sf = cfg.buildSessionFactory();
24
Session s = sf.openSession();
25
Transaction tx = s.beginTransaction();
26
Query q = s.createQuery( "from Boat" );
27
assertEquals( 0, q.list().size() );
28
q = s.createQuery( "from Plane" );
29
assertEquals( 0, q.list().size() );
35
public void testIgnoringHbm() throws Exception {
36
AnnotationConfiguration cfg = new AnnotationConfiguration();
37
cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
38
cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
39
cfg.setProperty( AnnotationConfiguration.ARTEFACT, "class, whatever" );
40
SessionFactory sf = cfg.buildSessionFactory();
42
Session s = sf.openSession();
43
Transaction tx = s.beginTransaction();
46
s.createQuery( "from Boat" ).list();
47
fail( "Boat should not be mapped" );
49
catch (HibernateException e) {
52
q = s.createQuery( "from Plane" );
53
assertEquals( 0, q.list().size() );
59
public void testPrecedenceHbm() throws Exception {
60
AnnotationConfiguration cfg = new AnnotationConfiguration();
61
cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
62
cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
63
cfg.addAnnotatedClass( Boat.class );
64
SessionFactory sf = cfg.buildSessionFactory();
66
Session s = sf.openSession();
67
s.getTransaction().begin();
68
Boat boat = new Boat();
72
s.getTransaction().commit();
74
Transaction tx = s.beginTransaction();
75
boat = (Boat) s.get( Boat.class, boat.getId() );
76
assertTrue( "Annotation has precedence", 34 != boat.getWeight() );
78
//s.getTransaction().commit();
84
public void testPrecedenceAnnotation() throws Exception {
85
AnnotationConfiguration cfg = new AnnotationConfiguration();
86
cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
87
cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
88
cfg.setProperty( AnnotationConfiguration.ARTEFACT, "class, hbm" );
89
cfg.addAnnotatedClass( Boat.class );
90
SessionFactory sf = cfg.buildSessionFactory();
92
Session s = sf.openSession();
93
s.getTransaction().begin();
94
Boat boat = new Boat();
98
s.getTransaction().commit();
100
Transaction tx = s.beginTransaction();
101
boat = (Boat) s.get( Boat.class, boat.getId() );
102
assertTrue( "Annotation has precedence", 34 == boat.getWeight() );
109
public void testDeclarativeAnnWoAnnConfig() throws Exception {
110
Configuration cfg = new Configuration();
112
cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
113
fail( "Configuration object should fail when finding annotated elements declarations" );
115
catch (MappingException e) {
120
public void testHbmWithSubclassExtends() throws Exception {
121
AnnotationConfiguration cfg = new AnnotationConfiguration();
122
cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
123
cfg.addClass( Ferry.class );
124
cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
125
SessionFactory sf = cfg.buildSessionFactory();
127
Session s = sf.openSession();
128
Transaction tx = s.beginTransaction();
129
Query q = s.createQuery( "from Ferry" );
130
assertEquals( 0, q.list().size() );
131
q = s.createQuery( "from Plane" );
132
assertEquals( 0, q.list().size() );
138
public void testAnnReferencesHbm() throws Exception {
139
AnnotationConfiguration cfg = new AnnotationConfiguration();
140
cfg.configure( "org/hibernate/test/annotations/hibernate.cfg.xml" );
141
cfg.addAnnotatedClass( Port.class );
142
cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
143
SessionFactory sf = cfg.buildSessionFactory();
145
Session s = sf.openSession();
146
Transaction tx = s.beginTransaction();
147
Query q = s.createQuery( "from Boat" );
148
assertEquals( 0, q.list().size() );
149
q = s.createQuery( "from Port" );
150
assertEquals( 0, q.list().size() );