-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: unidirectional one-to-many relationship
PostPosted: Tue Nov 09, 2004 9:32 pm 
Beginner
Beginner

Joined: Thu Oct 14, 2004 10:53 pm
Posts: 45
I have two classes, Patient and Report. In patient, there is a collection of Reports. Reports doesn't know about Patient, so the association is Unidirectional.

This is my mapping file definition for the Patinet object, Patient.hbm.xml

<bag name="reports" table="Report" cascade="all">
<key column="patientId"/>
<one-to-many class="com.orchestral.cdr.element.Report"/>
</bag>

and I've done it according to the documentation. However when I persist the patient object, it fails to write any child objects because it can't write the foreign key to the Reports table. The foreign-key is set to NOT-NULL, so it must write a value in that column. It keeps generating the below exception trace.

Am I missing something??? Please assist.


[b]Hibernate version:2.1.4 [/b]

[b]Mapping documents:
<bag name="reports" table="Report" cascade="all">
<key column="id"/>
<one-to-many class="com.orchestral.cdr.element.Report"/>
</bag>

[/b]

[b]Code between sessionFactory.openSession() and session.close():[/b]

[b]Full stack trace of any exception that occurs:
net.sf.hibernate.JDBCException: could not insert: [com.orchestral.cdr.element.Report#1]
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:478)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:442)
at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:29)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2414)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2367)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2236)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at com.orchestral.cdr.CDRDBBaseImpl.persist(CDRDBBaseImpl.java:285)
at com.orchestral.cdr.element.PersistReportTest.testReportPersistence(PersistReportTest.java:50)
at java.lang.reflect.Method.invoke(Native Method)
at junit.framework.TestCase.runTest(TestCase.java:166)
at junit.framework.TestCase.runBare(TestCase.java:140)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:131)
at junit.framework.TestSuite.runTest(TestSuite.java:173)
at junit.framework.TestSuite.run(TestSuite.java:168)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)
Caused by: java.sql.SQLException: [CONTRACT1]Cannot insert the value NULL into column 'patientID', table 'CDRAaron.dbo.Report'; column does not allow nulls. INSERT fails.
at com.inet.tds.e.a(Unknown Source)
at com.inet.tds.e.a(Unknown Source)
at com.inet.tds.b.int(Unknown Source)
at com.inet.tds.b.executeUpdate(Unknown Source)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:468)
... 20 more


[/b]

[b]Name and version of the database you are using:SQL Server 2000[/b]

[b]The generated SQL (show_sql=true):
Hibernate: insert into Patient (mothersMaidenNameString, sex, addressString, countyCode, homePhoneString, businessPhoneString, socialSecurityNumber, mothersIdentifierString, birthPlace, multipleBirthIndicator, birthOrder, patientDeathIndicator, dateOfBirth, patientDeathDate, livingDependency, livingArrangement, primaryFacilityName, primaryFacilityNameTypeCode, primaryFacilityNameRepresentationCode, primaryFacilityString, studentIndicator, handicap, livingWill, organDonor, separateBill, protectionIndicator, primaryFacilityID, driversLicenseNumber, issuingState, expiryDate, assigningAuthorityNamespaceID, assigningAuthorityUniversalID, assigningAuthorityUniversalIDType, medicalRecordNumber, checkDigit, checkDigitSchemeCode, identifierTypeCode, assigningFacilityNamespaceID, assigningFacilityUniversalID, assigningFacilityUniversalIDType, address, mothersMaidenName, homePhone, businessPhone, mothersIdentifier, patientAccountNumber, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into Report (id) values (?)
[/b]

[b]Debug level Hibernate log excerpt:[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 09, 2004 10:03 pm 
Beginner
Beginner

Joined: Thu Oct 14, 2004 10:53 pm
Posts: 45
Hi,

In response to my previous post, I had the FK column of the Report table set to NOT-NULL. When I set the attribute to allow NULL values, it works. However I want it to be set to NOT-NULL because a report cannot exist without a patient. How can I achieve this in hibernate using a Unidirectional mapping.

Also the mapping is incorrect. the correct one is
<bag name="reports" cascade="all">
<key column="patientID"/>
<one-to-many class="com.orchestral.cdr.element.Report"/>
</bag>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 10, 2004 1:46 am 
Regular
Regular

Joined: Thu Jul 01, 2004 12:13 am
Posts: 68
Location: San Diego, CA
You can't. I posted awhle ago about this and even tried to put in a JIRA request, but it simply can't be done. You must use bidirectional if you have FKs NOT NULL. You can see my whole post here: http://forum.hibernate.org/viewtopic.php?t=933131&highlight=

HTH,
Lou


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 10, 2004 4:26 pm 
Beginner
Beginner

Joined: Thu Oct 14, 2004 10:53 pm
Posts: 45
yes i suspected as much after much research and experimenting. thanks for your input.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 22, 2005 8:13 pm 
Newbie

Joined: Tue Feb 22, 2005 8:09 pm
Posts: 1
The latest release notes say that this is no longer the case.

http://www.hibernate.org/200.html

under the list of new mapping features, there a bullet that says:
support for unidirectional one-to-many associations mapped to a not-null foreign key

any idea how to actually make this work? I am having the same exact problem.

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.