-->
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.  [ 7 posts ] 
Author Message
 Post subject: Getting and ssigning column ID value....
PostPosted: Wed Mar 17, 2004 7:41 am 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
Hi,
I have 2 tables table1 and table2. Table1 has a primary key of tb1_id which acts as a foreign key in table2. In my maps, the ID for Table1 is sequenced and the ID for Table2 is assigned:

Table1:

<id name="ID" column="artist_id" type="integer" unsaved-value="0">
<generator class="sequence">
<param name="sequence">artist_seq_id</param>
</generator>
</id>

Table2:
<id name="ID" column="artist_id" type="integer" unsaved-value="0">
<generator class="assigned"/>
</id>

In my code I do the following:

Table1 tbl1 = new Table1();
Table2 tbl2 = new Table2();

... various tbl1 setters......

Now, what I want to do is assign the newly sequenced ID of Table1 to Table2. So I tried:

tbl2.setID(tbl1.getID());

tbl1.addNew(tbl1);
tbl2.addNewImages(tbl2);

All that happens is Table1 has a sequenced ID and Table2 has an ID value of 0. How do I resolve this?

many thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 17, 2004 7:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Take a look at the "foreign" generator.


Top
 Profile  
 
 Post subject: Michael...
PostPosted: Wed Mar 17, 2004 9:55 am 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
I have ammended both my maps but no records are being written to the respective tables. My maps are:

Artist(parent):

<hibernate-mapping package="test">
<class name="Artist" table="artisttbl">
<id name="ID" column="artist_id" type="integer" unsaved-value="0">
<generator class="sequence">
<param name="sequence">artist_seq_id</param>
</generator>
</id>
<property name="ArtistName" column="artist_name" type="string" not-null="true"/>
<property name="ArtistInfo" column="artist_info" type="string" not-null="true"/>
<property name="TrackLocation" column="track_location" type="string"/>
<property name="TrackInfo" column="track_info" type="string"/>

<one-to-one name="artistimages" class="ArtistImages"/>
</class>
</hibernate-mapping>


ArtistImages(child):

<hibernate-mapping package="test">
<class name="ArtistImages" table="artistimagestbl">
<id name="ID" column="artist_id">
<generator class="foreign">
<param name="property">artist</param>
</generator>
</id>
<property name="ArtistPhoto1" column="artist_photo1" type="binary"/>
<property name="ArtistPhoto2" column="artist_photo2" type="binary"/>
<property name="ArtistPhoto3" column="artist_photo3" type="binary"/>

<one-to-one name="artist" class="Artist" constrained="true"/>
</class>
</hibernate-mapping>

My code is:

Artist artist = new Artist();
ArtistImages newArt = new ArtistImages();

... setters for artist.....
...setters for artistimages

artist.addNewArtist(artist);
newArt.addNewArtistImages(newArt);

What am I missing?


Top
 Profile  
 
 Post subject: All...
PostPosted: Fri Mar 19, 2004 12:21 pm 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
has been resolved now. Thanks for your help.

Peter


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 19, 2004 2:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Would be nice if you could probably post how ...


Top
 Profile  
 
 Post subject: Michael...
PostPosted: Fri Mar 19, 2004 3:14 pm 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
The problem was an oversight on my behalf. The problem was in the one-to-one declaration in the Artist map (parent) to ArtistImages map (child). Instead of declaring the name as name="ArtistImages" I declared it as name="artistimages". Also, the addition of cascade="delete" helped as well! DUH!! The maps now read as :

Artist Map:Parent

<hibernate-mapping package="test">
<class name="Artist" table="artisttbl">
<id name="ID" column="artist_id">
<generator class="sequence">
<param name="sequence">artist_seq_id</param>
</generator>
</id>
..........
<one-to-one name="ArtistImages" class="ArtistImages" cascade="delete"/>
</class>
</hibernate-mapping>

ArtistImages Map:Child

<hibernate-mapping package="test">
<class name="ArtistImages" table="artistimagestbl">
<id name="ID" column="artist_id">
<generator class="foreign">
<param name="property">Artist</param>
</generator>
</id>
..........
<one-to-one name="Artist" class="Artist" constrained="true" cascade="delete"/>
</class>
</hibernate-mapping>

So to delete an associated record in both Artist and ArtistImages tables I do:

artist.deleteArtist(artist_id) or
artistimages.deleteArtistImages(artist_id)

Question? how do I define the Artist (parent) map as being lazy, in this instance, so that when i load an artist it does not load the child record until I physically request it? I have a static method, getArtist(), which I call in my code as:

Artist artist = Artist.getArtist(artist_id);

Peter


Top
 Profile  
 
 Post subject: Ok...
PostPosted: Fri Mar 19, 2004 4:09 pm 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
in an attempt to make my Artist (parent) map lazy, I amended it so that it reads as:

<hibernate-mapping package="test">
<class name="Artist" table="artisttbl" proxy="Artist">
<id name="ID" column="artist_id">
<generator class="sequence">
<param name="sequence">artist_seq_id</param>
</generator>
</id>
<property name="ArtistName" column="artist_name" type="string" not-null="true"/>
<property name="ArtistInfo" column="artist_info" type="string" not-null="true"/>
<property name="TrackLocation" column="track_location" type="string"/>
<property name="TrackInfo" column="track_info" type="string"/>
<one-to-one name="ArtistImages" constrained="true" outer-join="false" class="ArtistImages" cascade="delete"/>
</class>
</hibernate-mapping>

my ArtistImages (child) map reads as:

<hibernate-mapping package="test">
<class name="ArtistImages" table="artistimagestbl">
<id name="ID" column="artist_id">
<generator class="foreign">
<param name="property">Artist</param>
</generator>
</id>
<property name="ArtistPhoto1" column="artist_photo1" type="binary"/>
<property name="ArtistPhoto2" column="artist_photo2" type="binary"/>
<property name="ArtistPhoto3" column="artist_photo3" type="binary"/>
<one-to-one name="Artist" class="Artist" constrained="true" cascade="delete"/>
</class>
</hibernate-mapping>


I then tried to save a new artist record using the following code:
Artist artist = new Artist();
ArtistImages newArt = new ArtistImages();

artist.setArtistName("zzzzzz");
artist.setArtistInfo("zzzzzzzz");
artist.setTrackInfo("zzzzzzzz");
artist.setTrackLocation("zzzzzzz");

newArt.setArtistPhoto1(graph.readImage("1.jpg")); newArt.setArtistPhoto2(graph.readImage("2.jpg"));
newArt.setArtistPhoto3(graph.readImage("3.jpg"));

newArt.setArtist(artist);
artist.addNewArtist(artist);
newArt.addNewArtistImages(newArt);

Now I get an error which says:
Exception in Hibernate:: not-null property references a null or transient value: test.Artist.ArtistImages

Where am I going wrong?

Peter


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