GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / test / groovy / org / codehaus / groovy / grails / orm / hibernate / MappingDslTests.groovy
blobf7c18fa8a0de837833823a4f8ee5a90ee8745e2a
1 /**
2 * @author Graeme Rocher
3 * @since 1.0
4 *
5 * Created: Sep 27, 2007
6 */
7 package org.codehaus.groovy.grails.orm.hibernate
9 import javax.sql.DataSource
10 import org.hibernate.SessionFactory
12 class MappingDslTests extends AbstractGrailsHibernateTests {
14 void testTableMapping() {
15 DataSource ds = (DataSource)applicationContext.getBean('dataSource')
17 def con
18 try {
19 con = ds.getConnection()
20 def statement = con.prepareStatement("select * from people")
21 statement.execute()
22 } finally {
23 con.close()
27 void testColumnNameMappings() {
28 def p = ga.getDomainClass("PersonDSL").newInstance()
29 p.firstName = "Wilma"
30 p.save()
31 session.flush()
33 DataSource ds = (DataSource)applicationContext.getBean('dataSource')
35 def con
36 try {
37 con = ds.getConnection()
38 def statement = con.prepareStatement("select First_Name from people")
39 def result = statement.executeQuery()
40 assert result.next()
41 def name = result.getString('First_Name')
43 assertEquals "Wilma", name
45 } finally {
46 con.close()
50 void testDisabledVersion() {
51 def p = ga.getDomainClass("PersonDSL").newInstance()
52 p.firstName = "Wilma"
53 p.save()
54 session.flush()
57 assertNull p.version
60 void testEnabledVersion() {
61 def p = ga.getDomainClass("PersonDSL2").newInstance()
62 p.firstName = "Wilma"
63 p.save()
64 session.flush()
66 assertEquals 0, p.version
68 p.firstName = "Bob"
69 p.save()
70 session.flush()
72 assertEquals 1, p.version
76 void testCustomHiLoIdGenerator() {
77 def p = ga.getDomainClass("PersonDSL").newInstance()
78 p.firstName = "Wilma"
79 p.save()
80 session.flush()
82 assert p.id
83 DataSource ds = (DataSource)applicationContext.getBean('dataSource')
85 def con
86 try {
87 con = ds.getConnection()
88 def statement = con.prepareStatement("select * from hi_value")
89 def result = statement.executeQuery()
90 assert result.next()
91 def value = result.getLong('next_value')
93 assertEquals 1, value
95 } finally {
96 con.close()
102 void testLazyinessControl() {
103 def personClass = ga.getDomainClass("PersonDSL")
104 def p = personClass.newInstance()
105 p.firstName = "Wilma"
107 println "SAVING PERSON"
108 p.save(flush:true)
109 p.addToChildren(firstName:"Dino", lastName:'Dinosaur')
110 p.addToCousins(firstName:"Bob", lastName:'The Builder')
111 println "SAVING RELATIONS"
112 p.save(flush:true)
113 session.clear()
115 p = personClass.clazz.get(1)
117 assertTrue p.children.wasInitialized()
118 assertFalse p.cousins.wasInitialized()
122 void testUserTypes() {
123 DataSource ds = (DataSource)applicationContext.getBean('dataSource')
124 def relativeClass = ga.getDomainClass("Relative")
125 def r = relativeClass.newInstance()
126 r.firstName = "Wilma"
127 r.lastName = 'Flintstone'
128 r.save()
129 session.flush()
131 def con
132 try {
133 con = ds.getConnection()
134 def statement = con.prepareStatement("select * from relative")
135 def result = statement.executeQuery()
136 assert result.next()
137 def metadata = result.getMetaData()
138 assertEquals "FIRST_NAME",metadata.getColumnLabel(3)
139 // hsqldb returns -1 for text type, if it wasn't mapped as text it would be 12 so this is an ok test
140 assertEquals( -1, metadata.getColumnType(3) )
143 } finally {
144 con.close()
149 void testCompositeIdMapping() {
150 def compositePersonClass = ga.getDomainClass("CompositePerson")
151 def cp = compositePersonClass.newInstance()
153 cp.firstName = "Fred"
154 cp.lastName = "Flintstone"
155 cp.save()
156 session.flush()
157 session.clear()
159 cp = compositePersonClass.newInstance()
161 cp.firstName = "Fred"
162 cp.lastName = "Flintstone"
164 def cp1 = compositePersonClass.clazz.get(cp)
166 assert cp1
167 assertEquals "Fred", cp1.firstName
168 assertEquals "Flintstone", cp1.lastName
171 void testTablePerSubclassInheritance() {
172 DataSource ds = (DataSource)applicationContext.getBean('dataSource')
174 def con
175 try {
176 con = ds.getConnection()
177 def statement = con.prepareStatement("select * from payment")
178 statement.execute()
179 statement = con.prepareStatement("select * from credit_card_payment")
180 statement.execute()
182 } finally {
183 con.close()
186 def p = ga.getDomainClass("Payment").newInstance()
188 p.amount = 10
189 p.save()
190 session.flush()
192 def ccp = ga.getDomainClass("CreditCardPayment").newInstance()
193 ccp.amount = 20
194 ccp.cardNumber = "43438094834380"
195 ccp.save()
196 session.flush()
198 session.clear()
200 ccp = ga.getDomainClass("CreditCardPayment").clazz.findByAmount(20)
202 assert ccp
203 assertEquals 20, ccp.amount
204 assertEquals "43438094834380", ccp.cardNumber
207 void testOneToOneForeignKeyMapping() {
208 def personClass = ga.getDomainClass("MappedPerson").clazz
209 def addressClass = ga.getDomainClass("MappedAddress").clazz
211 def p = personClass.newInstance(name:"John")
212 p.address = addressClass.newInstance()
215 assert p.save()
216 session.flush()
218 DataSource ds = (DataSource)applicationContext.getBean('dataSource')
220 def con
221 try {
222 con = ds.getConnection()
223 def statement = con.prepareStatement("select PERSON_ADDRESS_COLUMN from mapped_person")
224 def resultSet = statement.executeQuery()
225 assert resultSet.next()
226 } finally {
227 con.close()
231 void testManyToOneForeignKeyMapping() {
232 def personClass = ga.getDomainClass("MappedPerson").clazz
233 def groupClass = ga.getDomainClass("MappedGroup").clazz
235 def g = groupClass.newInstance()
237 g.addToPeople name:"John"
239 assert g.save()
241 session.flush()
242 session.clear()
244 g = groupClass.get(1)
246 assert g
247 assertEquals 1, g.people.size()
249 DataSource ds = (DataSource)applicationContext.getBean('dataSource')
251 def con
252 try {
253 con = ds.getConnection()
254 def statement = con.prepareStatement("select PERSON_GROUP_COLUMN from mapped_person")
255 def resultSet = statement.executeQuery()
256 assert resultSet.next()
257 } finally {
258 con.close()
263 void testManyToManyForeignKeyMapping() {
264 def partnerClass = ga.getDomainClass("MappedPartner").clazz
265 def groupClass = ga.getDomainClass("MappedGroup").clazz
267 def g = groupClass.newInstance()
269 g.addToPartners(partnerClass.newInstance())
271 assert g.save()
272 session.flush()
273 session.clear()
275 g = groupClass.get(1)
277 assert g
278 assertEquals 1, g.partners.size()
280 DataSource ds = (DataSource)applicationContext.getBean('dataSource')
282 def con
283 try {
284 con = ds.getConnection()
285 def statement = con.prepareStatement("select PARTNER_JOIN_COLUMN,GROUP_JOIN_COLUMN from PARTNER_GROUP_ASSOCIATIONS")
286 def resultSet = statement.executeQuery()
287 assert resultSet.next()
288 } finally {
289 con?.close()
293 void testUnidirectionalOneToManyForeignKeyMapping() {
294 def personClass = ga.getDomainClass("MappedPerson").clazz
295 def childClass = ga.getDomainClass("MappedChild").clazz
297 def p = personClass.newInstance(name:"John")
299 p.addToChildren(childClass.newInstance())
300 p.addToCousins(childClass.newInstance())
301 p.save()
304 assert p.save()
305 session.flush()
307 DataSource ds = (DataSource)applicationContext.getBean('dataSource')
309 def con
310 try {
311 con = ds.getConnection()
312 def statement = con.prepareStatement("select PERSON_ID,COUSIN_ID from COUSINS_TABLE")
313 def resultSet = statement.executeQuery()
314 assert resultSet.next()
315 } finally {
316 con.close()
322 protected void onSetUp() {
323 gcl.parseClass('''
324 class MappedPerson {
325 Long id
326 Long version
327 String name
328 MappedAddress address
329 MappedGroup group
330 Set children
331 Set cousins
333 static hasMany = [children:MappedChild, cousins:MappedChild]
334 static belongsTo = MappedGroup
335 static mapping = {
336 columns {
337 address column:'PERSON_ADDRESS_COLUMN'
338 group column:'PERSON_GROUP_COLUMN'
339 children column:'PERSON_CHILD_ID'
340 cousins joinTable:[name:'COUSINS_TABLE', key:'PERSON_ID', column:'COUSIN_ID']
343 static constraints = {
344 group(nullable:true)
345 address(nullable:true)
349 class MappedChild {
350 Long id
351 Long version
353 class MappedAddress {
354 Long id
355 Long version
357 static belongsTo = MappedPerson
360 class MappedGroup {
361 Long id
362 Long version
364 Set people
365 Set partners
366 static hasMany = [people:MappedPerson, partners:MappedPartner]
367 static mapping = {
368 columns {
369 partners column:'PARTNER_JOIN_COLUMN', joinTable:'PARTNER_GROUP_ASSOCIATIONS'
374 class MappedPartner {
375 Long id
376 Long version
378 Set groups
379 static belongsTo = MappedGroup
380 static hasMany = [groups:MappedGroup]
381 static mapping = {
382 columns {
383 groups column:'GROUP_JOIN_COLUMN', joinTable:'PARTNER_GROUP_ASSOCIATIONS'
388 class Payment {
389 Long id
390 Long version
391 Integer amount
393 static mapping = {
394 tablePerHierarchy false
397 class CreditCardPayment extends Payment {
398 String cardNumber
401 class CompositePerson implements Serializable {
402 Long id
403 Long version
404 String firstName
405 String lastName
407 static mapping = {
408 id composite:['firstName', 'lastName']
411 class PersonDSL {
412 Long id
413 Long version
414 String firstName
415 Set children
416 Set cousins
418 static hasMany = [children:Relative, cousins:Relative]
419 static mapping = {
420 table 'people'
421 version false
422 cache usage:'read-only', include:'non-lazy'
423 id generator:'hilo', params:[table:'hi_value',column:'next_value',max_lo:100]
425 columns {
426 firstName name:'First_Name'
427 children lazy:false, cache:'read-write\'
432 class Relative {
433 Long id
434 Long version
436 String firstName
437 String lastName
439 static mapping = {
440 columns {
441 firstName type:'text', index:'name_index'
442 lastName index:'name_index,other_index'
447 class PersonDSL2 {
448 Long id
449 Long version
450 String firstName
452 static mapping = {
453 table 'people2'
454 version true
455 cache usage:'read-write', include:'non-lazy'
456 id column:'person_id'
458 columns {
459 firstName name:'First_Name'
463 ''')