PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/grails/src/test/org/codehaus/groovy/grails/orm/hibernate/MappingDslTests.groovy

https://github.com/jabley/grails
Groovy | 469 lines | 356 code | 106 blank | 7 comment | 0 complexity | 7d24d615188abaa3b1e8c1227c8bc6a8 MD5 | raw file
  1. /**
  2. * @author Graeme Rocher
  3. * @since 1.0
  4. *
  5. * Created: Sep 27, 2007
  6. */
  7. package org.codehaus.groovy.grails.orm.hibernate
  8. import javax.sql.DataSource
  9. import org.hibernate.SessionFactory
  10. class MappingDslTests extends AbstractGrailsHibernateTests {
  11. void testTableMapping() {
  12. DataSource ds = (DataSource)applicationContext.getBean('dataSource')
  13. def con
  14. try {
  15. con = ds.getConnection()
  16. def statement = con.prepareStatement("select * from people")
  17. statement.execute()
  18. } finally {
  19. con.close()
  20. }
  21. }
  22. void testColumnNameMappings() {
  23. def p = ga.getDomainClass("PersonDSL").newInstance()
  24. p.firstName = "Wilma"
  25. p.save()
  26. session.flush()
  27. DataSource ds = (DataSource)applicationContext.getBean('dataSource')
  28. def con
  29. try {
  30. con = ds.getConnection()
  31. def statement = con.prepareStatement("select First_Name from people")
  32. def result = statement.executeQuery()
  33. assert result.next()
  34. def name = result.getString('First_Name')
  35. assertEquals "Wilma", name
  36. } finally {
  37. con.close()
  38. }
  39. }
  40. void testDisabledVersion() {
  41. def p = ga.getDomainClass("PersonDSL").newInstance()
  42. p.firstName = "Wilma"
  43. p.save()
  44. session.flush()
  45. assertNull p.version
  46. }
  47. void testEnabledVersion() {
  48. def p = ga.getDomainClass("PersonDSL2").newInstance()
  49. p.firstName = "Wilma"
  50. p.save()
  51. session.flush()
  52. assertEquals 0, p.version
  53. p.firstName = "Bob"
  54. p.save()
  55. session.flush()
  56. assertEquals 1, p.version
  57. }
  58. void testCustomHiLoIdGenerator() {
  59. def p = ga.getDomainClass("PersonDSL").newInstance()
  60. p.firstName = "Wilma"
  61. p.save()
  62. session.flush()
  63. assert p.id
  64. DataSource ds = (DataSource)applicationContext.getBean('dataSource')
  65. def con
  66. try {
  67. con = ds.getConnection()
  68. def statement = con.prepareStatement("select * from hi_value")
  69. def result = statement.executeQuery()
  70. assert result.next()
  71. def value = result.getLong('next_value')
  72. assertEquals 1, value
  73. } finally {
  74. con.close()
  75. }
  76. }
  77. void testLazyinessControl() {
  78. def personClass = ga.getDomainClass("PersonDSL")
  79. def p = personClass.newInstance()
  80. p.firstName = "Wilma"
  81. println "SAVING PERSON"
  82. p.save(flush:true)
  83. p.addToChildren(firstName:"Dino", lastName:'Dinosaur')
  84. p.addToCousins(firstName:"Bob", lastName:'The Builder')
  85. println "SAVING RELATIONS"
  86. p.save(flush:true)
  87. session.clear()
  88. p = personClass.clazz.get(1)
  89. assertTrue p.children.wasInitialized()
  90. assertFalse p.cousins.wasInitialized()
  91. }
  92. void testUserTypes() {
  93. DataSource ds = (DataSource)applicationContext.getBean('dataSource')
  94. def relativeClass = ga.getDomainClass("Relative")
  95. def r = relativeClass.newInstance()
  96. r.firstName = "Wilma"
  97. r.lastName = 'Flintstone'
  98. r.save()
  99. session.flush()
  100. def con
  101. try {
  102. con = ds.getConnection()
  103. def statement = con.prepareStatement("select * from relative")
  104. def result = statement.executeQuery()
  105. assert result.next()
  106. def metadata = result.getMetaData()
  107. assertEquals "FIRST_NAME",metadata.getColumnLabel(4)
  108. // hsqldb returns -1 for text type, if it wasn't mapped as text it would be 12 so this is an ok test
  109. assertEquals( -1, metadata.getColumnType(4) )
  110. } finally {
  111. con.close()
  112. }
  113. }
  114. void testCompositeIdMapping() {
  115. def compositePersonClass = ga.getDomainClass("CompositePerson")
  116. def cp = compositePersonClass.newInstance()
  117. cp.firstName = "Fred"
  118. cp.lastName = "Flintstone"
  119. cp.save()
  120. session.flush()
  121. session.clear()
  122. cp = compositePersonClass.newInstance()
  123. cp.firstName = "Fred"
  124. cp.lastName = "Flintstone"
  125. def cp1 = compositePersonClass.clazz.get(cp)
  126. assert cp1
  127. assertEquals "Fred", cp1.firstName
  128. assertEquals "Flintstone", cp1.lastName
  129. }
  130. void testTablePerSubclassInheritance() {
  131. DataSource ds = (DataSource)applicationContext.getBean('dataSource')
  132. def con
  133. try {
  134. con = ds.getConnection()
  135. def statement = con.prepareStatement("select * from payment")
  136. statement.execute()
  137. statement = con.prepareStatement("select * from credit_card_payment")
  138. statement.execute()
  139. } finally {
  140. con.close()
  141. }
  142. def p = ga.getDomainClass("Payment").newInstance()
  143. p.amount = 10
  144. p.save()
  145. session.flush()
  146. def ccp = ga.getDomainClass("CreditCardPayment").newInstance()
  147. ccp.amount = 20
  148. ccp.cardNumber = "43438094834380"
  149. ccp.save()
  150. session.flush()
  151. session.clear()
  152. ccp = ga.getDomainClass("CreditCardPayment").clazz.findByAmount(20)
  153. assert ccp
  154. assertEquals 20, ccp.amount
  155. assertEquals "43438094834380", ccp.cardNumber
  156. }
  157. void testOneToOneForeignKeyMapping() {
  158. def personClass = ga.getDomainClass("MappedPerson").clazz
  159. def addressClass = ga.getDomainClass("MappedAddress").clazz
  160. def p = personClass.newInstance(name:"John")
  161. p.address = addressClass.newInstance()
  162. assert p.save()
  163. session.flush()
  164. DataSource ds = (DataSource)applicationContext.getBean('dataSource')
  165. def con
  166. try {
  167. con = ds.getConnection()
  168. def statement = con.prepareStatement("select PERSON_ADDRESS_COLUMN from mapped_person")
  169. def resultSet = statement.executeQuery()
  170. assert resultSet.next()
  171. } finally {
  172. con.close()
  173. }
  174. }
  175. void testManyToOneForeignKeyMapping() {
  176. def personClass = ga.getDomainClass("MappedPerson").clazz
  177. def groupClass = ga.getDomainClass("MappedGroup").clazz
  178. def g = groupClass.newInstance()
  179. g.addToPeople name:"John"
  180. assert g.save()
  181. session.flush()
  182. session.clear()
  183. g = groupClass.get(1)
  184. assert g
  185. assertEquals 1, g.people.size()
  186. DataSource ds = (DataSource)applicationContext.getBean('dataSource')
  187. def con
  188. try {
  189. con = ds.getConnection()
  190. def statement = con.prepareStatement("select PERSON_GROUP_COLUMN from mapped_person")
  191. def resultSet = statement.executeQuery()
  192. assert resultSet.next()
  193. } finally {
  194. con.close()
  195. }
  196. }
  197. void testManyToManyForeignKeyMapping() {
  198. def partnerClass = ga.getDomainClass("MappedPartner").clazz
  199. def groupClass = ga.getDomainClass("MappedGroup").clazz
  200. def g = groupClass.newInstance()
  201. g.addToPartners(partnerClass.newInstance())
  202. assert g.save()
  203. session.flush()
  204. session.clear()
  205. g = groupClass.get(1)
  206. assert g
  207. assertEquals 1, g.partners.size()
  208. DataSource ds = (DataSource)applicationContext.getBean('dataSource')
  209. def con
  210. try {
  211. con = ds.getConnection()
  212. def statement = con.prepareStatement("select PARTNER_JOIN_COLUMN,GROUP_JOIN_COLUMN from PARTNER_GROUP_ASSOCIATIONS")
  213. def resultSet = statement.executeQuery()
  214. assert resultSet.next()
  215. } finally {
  216. con?.close()
  217. }
  218. }
  219. void testUnidirectionalOneToManyForeignKeyMapping() {
  220. def personClass = ga.getDomainClass("MappedPerson").clazz
  221. def childClass = ga.getDomainClass("MappedChild").clazz
  222. def p = personClass.newInstance(name:"John")
  223. p.addToChildren(childClass.newInstance())
  224. p.addToCousins(childClass.newInstance())
  225. p.save()
  226. assert p.save()
  227. session.flush()
  228. DataSource ds = (DataSource)applicationContext.getBean('dataSource')
  229. def con
  230. try {
  231. con = ds.getConnection()
  232. def statement = con.prepareStatement("select PERSON_ID,COUSIN_ID from COUSINS_TABLE")
  233. def resultSet = statement.executeQuery()
  234. assert resultSet.next()
  235. } finally {
  236. con.close()
  237. }
  238. }
  239. protected void onSetUp() {
  240. gcl.parseClass('''
  241. class MappedPerson {
  242. Long id
  243. Long version
  244. String name
  245. MappedAddress address
  246. MappedGroup group
  247. Set children
  248. Set cousins
  249. static hasMany = [children:MappedChild, cousins:MappedChild]
  250. static belongsTo = MappedGroup
  251. static mapping = {
  252. columns {
  253. address column:'PERSON_ADDRESS_COLUMN'
  254. group column:'PERSON_GROUP_COLUMN'
  255. children column:'PERSON_CHILD_ID'
  256. cousins joinTable:[name:'COUSINS_TABLE', key:'PERSON_ID', column:'COUSIN_ID']
  257. }
  258. }
  259. static constraints = {
  260. group(nullable:true)
  261. address(nullable:true)
  262. }
  263. }
  264. class MappedChild {
  265. Long id
  266. Long version
  267. }
  268. class MappedAddress {
  269. Long id
  270. Long version
  271. static belongsTo = MappedPerson
  272. }
  273. class MappedGroup {
  274. Long id
  275. Long version
  276. Set people
  277. Set partners
  278. static hasMany = [people:MappedPerson, partners:MappedPartner]
  279. static mapping = {
  280. columns {
  281. partners column:'PARTNER_JOIN_COLUMN', joinTable:'PARTNER_GROUP_ASSOCIATIONS'
  282. }
  283. }
  284. }
  285. class MappedPartner {
  286. Long id
  287. Long version
  288. Set groups
  289. static belongsTo = MappedGroup
  290. static hasMany = [groups:MappedGroup]
  291. static mapping = {
  292. columns {
  293. groups column:'GROUP_JOIN_COLUMN', joinTable:'PARTNER_GROUP_ASSOCIATIONS'
  294. }
  295. }
  296. }
  297. class Payment {
  298. Long id
  299. Long version
  300. Integer amount
  301. static mapping = {
  302. tablePerHierarchy false
  303. }
  304. }
  305. class CreditCardPayment extends Payment {
  306. String cardNumber
  307. }
  308. class CompositePerson implements Serializable {
  309. Long id
  310. Long version
  311. String firstName
  312. String lastName
  313. static mapping = {
  314. id composite:['firstName', 'lastName']
  315. }
  316. }
  317. class PersonDSL {
  318. Long id
  319. Long version
  320. String firstName
  321. Set children
  322. Set cousins
  323. static hasMany = [children:Relative, cousins:Relative]
  324. static mapping = {
  325. table 'people'
  326. version false
  327. cache usage:'read-only', include:'non-lazy'
  328. id generator:'hilo', params:[table:'hi_value',column:'next_value',max_lo:100]
  329. columns {
  330. firstName name:'First_Name'
  331. children lazy:false, cache:'read-write\'
  332. }
  333. }
  334. }
  335. class Relative {
  336. Long id
  337. Long version
  338. String firstName
  339. String lastName
  340. static mapping = {
  341. columns {
  342. firstName type:'text', index:'name_index'
  343. lastName index:'name_index,other_index'
  344. }
  345. }
  346. }
  347. class PersonDSL2 {
  348. Long id
  349. Long version
  350. String firstName
  351. static mapping = {
  352. table 'people2'
  353. version true
  354. cache usage:'read-write', include:'non-lazy'
  355. id column:'person_id'
  356. columns {
  357. firstName name:'First_Name'
  358. }
  359. }
  360. }
  361. ''')
  362. }
  363. }