-->
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.  [ 4 posts ] 
Author Message
 Post subject: Mapping Problem
PostPosted: Sat Jul 26, 2008 1:59 pm 
Newbie

Joined: Sun Apr 27, 2008 3:30 pm
Posts: 7
Hibernate version:

3.2.6.ga

Mapping documents:

***Account.hbm.xml***

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="wrappers.Account" table="Account">
<cache usage="read-only"/>
<id name="id" column="id">
<generator class="sequence">
<param name="sequence">hibernate_sequence</param>
</generator>
</id>

<property name="credit" />
<property name="discount" />
<property name="openedDate" />
<property name="closedDate" />

<one-to-one name="loginDetails"
class="wrappers.LoginDetails"
cascade="all"
property-ref="account"
/>

<one-to-one name="userDetails"
class="wrappers.UserDetails"
cascade="all"
property-ref="account"
/>
</class>
</hibernate-mapping>

***LoginDetails.hbm.xml***

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="wrappers.LoginDetails" table="LoginDetails">
<cache usage="read-only"/>
<id name="id" column="id">
<generator class="sequence">
<param name="sequence">hibernate_sequence</param>
</generator>
</id>

<property name="username" />
<property name="password" />

<one-to-one name="account" class="wrappers.Account" constrained="true" />

</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(account);
session.getTransaction().commit();

Full stack trace of any exception that occurs:

org.hibernate.PropertyValueException: not-null property references a null or transient value: wrappers.LoginDetails.account

Name and version of the database you are using:

Postgresql 8.2.


There has to be a problem with my mappings.. something silly I just cannot see.

My tables are

Account (id, credit, discount, openedDate, closedDate)
LoginDetails (id, username, password, accountId)

where id are primary keys in both, and accountId is foreign key to account. There are no foreign key from Account to the LoginDetails.

It is a 1-1 relationship.

I am creating an Account Object and populating it. Creating a LoginDetails object and populating it, and then setting it into the Account. So everything is set except both primary keys (which I have set in my mappings to use a sequence number), and the foreign key accountId in the LoginDetails. It seems this key is the problematic one.

How can I fix this so I can create both objects and save as one and have the keys all taken care of.

thanks very much in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 27, 2008 11:33 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Have you tried saving both sides of the association? Perhaps even ensuring that you have a CascadeType of All setting might help as well.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 27, 2008 6:08 pm 
Newbie

Joined: Sun Apr 27, 2008 3:30 pm
Posts: 7
Not sure exactly what you mean, but I dont think that is what I am looking for.

I cannot even do what I envision to be very basic queries.

I have tried:

select account from Account as account, LoginDetails as loginDetails where account.id = loginDetails.accountId and loginDetails.username = :username

This only extracts the Account, and not the children (LoginDetails and UserDetails).

I have tried:

from Account as account where account.loginDetails.username = :username

This tries to do a join with account.id = loginDetails.id which will not bring back any results because they are not related by their ids. LoginDetails maps to account by its accountId.

I have been at this for 15 hours now today and I am getting absolutely nowhere. Any help on this would be greatly apreciated. :(

My mappings are now.

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="cakeinabox.wrappers.Account" table="Account">
        <cache usage="read-only"/>
        <id name="id" column="id">
            <generator class="sequence">
                 <param name="sequence">hibernate_sequence</param>
             </generator>
        </id>

        <property name="credit" />
        <property name="discount" />
        <property name="openedDate" />
        <property name="closedDate" />

        <one-to-one name="loginDetails" class="cakeinabox.wrappers.LoginDetails" cascade="all" lazy="false" />
        <one-to-one name="userDetails" class="cakeinabox.wrappers.UserDetails" cascade="all" lazy="false"/>

    </class>
</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="cakeinabox.wrappers.LoginDetails" table="LoginDetails">
        <cache usage="read-only"/>
        <id name="id" column="id">
            <generator class="sequence">
                 <param name="sequence">hibernate_sequence</param>
             </generator>
        </id>

        <property name="username" />
        <property name="password" />

        <property name="accountId" update="false" insert="false" />

        <many-to-one name="account" class="cakeinabox.wrappers.Account" cascade="all" column="accountId" lazy="false" />

    </class>
</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="cakeinabox.wrappers.UserDetails" table="UserDetails">
        <cache usage="read-only"/>
        <id name="id" column="id">
            <generator class="sequence">
                 <param name="sequence">hibernate_sequence</param>
             </generator>
        </id>

        <property name="firstName" />
        <property name="lastName" />
        <property name="addressLine1" />
        <property name="addressLine2" />
        <property name="city" />
        <property name="county" />
        <property name="telephoneNumber" />
        <property name="emailAddress" />
        <property name="postCode" />
        <property name="country" />

        <property name="accountId" update="false" insert="false" />

        <many-to-one name="account" class="cakeinabox.wrappers.Account" cascade="all" column="accountId" lazy="false" />

    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 28, 2008 5:43 pm 
Newbie

Joined: Sun Apr 27, 2008 3:30 pm
Posts: 7
Lets take this back a level.....

Say I have my relation Account, an account has-a LoginDetails, on a 1-to-1 relationship.

Is it best to have the for LoginDetails have the foreign key to the Account, vice versa or both?

I want to be able to query "from Account" and be able to fetch the LoginDetails component with

account.getLoginDetails()

I also want to be able to query "from Account as account where account.loginDetails.username = :username'

I tried to fetch an account this way using the above mappings but it tried to join account to logindetails by both of their primary keys which is not correct as logindetails relates to the account by accountid.

Hope this helps clear up my requirements.

thanks

Chris


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.