1
//$Id: CreateTest.java 10976 2006-12-12 23:22:26Z steve.ebersole@jboss.com $
2
package org.hibernate.test.ops;
4
import java.util.ArrayList;
5
import java.util.Collection;
7
import junit.framework.Test;
9
import org.hibernate.PersistentObjectException;
10
import org.hibernate.Session;
11
import org.hibernate.Transaction;
12
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
13
import org.hibernate.exception.ConstraintViolationException;
18
public class CreateTest extends AbstractOperationTestCase {
20
public CreateTest(String str) {
24
public static Test suite() {
25
return new FunctionalTestClassTestSuite( CreateTest.class );
28
public void testNoUpdatesOnCreateVersionedWithCollection() {
31
Session s = openSession();
32
Transaction tx = s.beginTransaction();
33
VersionedEntity root = new VersionedEntity( "root", "root" );
34
VersionedEntity child = new VersionedEntity( "c1", "child-1" );
35
root.getChildren().add( child );
36
child.setParent( root );
41
assertInsertCount( 2 );
42
assertUpdateCount( 0 );
43
assertDeleteCount( 0 );
46
tx = s.beginTransaction();
51
assertUpdateCount( 0 );
52
assertDeleteCount( 2 );
55
public void testCreateTree() {
59
Session s = openSession();
60
Transaction tx = s.beginTransaction();
61
Node root = new Node("root");
62
Node child = new Node("child");
72
tx = s.beginTransaction();
73
System.out.println("getting");
74
root = (Node) s.get(Node.class, "root");
75
Node child2 = new Node("child2");
76
root.addChild(child2);
77
System.out.println("committing");
85
public void testCreateTreeWithGeneratedId() {
89
Session s = openSession();
90
Transaction tx = s.beginTransaction();
91
NumberedNode root = new NumberedNode("root");
92
NumberedNode child = new NumberedNode("child");
102
tx = s.beginTransaction();
103
root = (NumberedNode) s.get( NumberedNode.class, new Long( root.getId() ) );
104
NumberedNode child2 = new NumberedNode("child2");
105
root.addChild(child2);
109
assertInsertCount(3);
110
assertUpdateCount(0);
113
public void testCreateException() {
114
Session s = openSession();
115
Transaction tx = s.beginTransaction();
116
Node dupe = new Node("dupe");
123
tx = s.beginTransaction();
129
catch (ConstraintViolationException cve) {
130
//verify that an exception is thrown!
135
Node nondupe = new Node("nondupe");
136
nondupe.addChild(dupe);
139
tx = s.beginTransaction();
145
catch (ConstraintViolationException cve) {
146
//verify that an exception is thrown!
152
public void testCreateExceptionWithGeneratedId() {
153
Session s = openSession();
154
Transaction tx = s.beginTransaction();
155
NumberedNode dupe = new NumberedNode("dupe");
162
tx = s.beginTransaction();
167
catch (PersistentObjectException poe) {
168
//verify that an exception is thrown!
173
NumberedNode nondupe = new NumberedNode("nondupe");
174
nondupe.addChild(dupe);
177
tx = s.beginTransaction();
182
catch (PersistentObjectException poe) {
183
//verify that an exception is thrown!
189
public void testBasic() throws Exception {
193
tx = s.beginTransaction();
194
Employer er = new Employer();
195
Employee ee = new Employee();
197
Collection erColl = new ArrayList();
198
Collection eeColl = new ArrayList();
201
er.setEmployees(erColl);
202
ee.setEmployers(eeColl);
207
tx = s.beginTransaction();
208
er = (Employer) s.load(Employer.class, er.getId() );
210
assertNotNull( er.getEmployees() );
211
assertEquals( 1, er.getEmployees().size() );
212
Employee eeFromDb = (Employee) er.getEmployees().iterator().next();
213
assertEquals( ee.getId(), eeFromDb.getId() );