-->
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.  [ 3 posts ] 
Author Message
 Post subject: not-null property references a null or transient value
PostPosted: Fri Feb 18, 2005 8:40 am 
Beginner
Beginner

Joined: Tue Feb 15, 2005 8:14 am
Posts: 32
I am trying to insert a Team with some players.
But the not-null constrain is causing exception.

If I change the not-null="true" to not-null="false" in the hibernate.many-to-one relation in Player the players are inserted bu offcause with a team id....




Hibernate version:
2.1

Mapping documents:


*************' TEAM *************
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping
>
<class
name="dk.te.deb.pojo.Team"
table="TEAM"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>

<id
name="teamId"
column="TEAM_ID"
type="java.lang.Long"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Team.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>

<set
name="players"
lazy="true"
inverse="true"
cascade="all"
sort="unsorted"
>

<key
column="TEAM_ID"
>
</key>

<one-to-many
class="dk.te.deb.pojo.Player"
/>

</set>

<property
name="city"
type="string"
update="true"
insert="true"
access="property"
column="CITY"
length="20"
not-null="false"
unique="false"
/>

<property
name="teamName"
type="string"
update="true"
insert="true"
access="property"
column="TEAM_NAME"
length="20"
not-null="false"
unique="false"
/>

<property
name="endDate"
type="timestamp"
update="true"
insert="true"
access="property"
column="END_DATE"
not-null="true"
unique="false"
/>

<property
name="startDate"
type="timestamp"
update="true"
insert="true"
access="property"
column="START_DATE"
not-null="true"
unique="false"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Team.xml
containing the additional properties and place it in your merge dir.
-->

</class>

<query name="findIdByTeamName"><![CDATA[
select team.team_id from dk.te.deb.pojo.Team team where team.team_name = :teamName
]]></query>
<query name="findAllTeams"><![CDATA[
from dk.te.deb.pojo.Team
]]></query>

</hibernate-mapping>


************** PLAYER **************
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping
>
<class
name="dk.te.deb.pojo.Player"
table="PLAYER"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>

<id
name="playerId"
column="PLAYER_ID"
type="long"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Player.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>

<many-to-one
name="team"
class="dk.te.deb.pojo.Team"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="TEAM_ID"
not-null="true"
/>

<property
name="teamId"
type="long"
update="false"
insert="false"
access="property"
column="TEAM_ID"
unique="false"
/>

<property
name="annualSalary"
type="float"
update="true"
insert="true"
access="property"
column="ANNUAL_SALARY"
not-null="false"
unique="false"
/>

<property
name="draftDate"
type="timestamp"
update="true"
insert="true"
access="property"
column="DRAFT_DATE"
not-null="false"
unique="false"
/>

<property
name="firstName"
type="string"
update="true"
insert="true"
access="property"
column="FIRST_NAME"
length="20"
not-null="true"
unique="false"
/>

<property
name="jerseyNumber"
type="int"
update="true"
insert="true"
access="property"
column="JERSEY_NUMBER"
not-null="true"
unique="false"
/>

<property
name="lastName"
type="string"
update="true"
insert="true"
access="property"
column="LAST_NAME"
length="20"
not-null="true"
unique="false"
/>

<property
name="endDate"
type="timestamp"
update="true"
insert="true"
access="property"
column="END_DATE"
not-null="true"
unique="false"
/>

<property
name="startDate"
type="timestamp"
update="true"
insert="true"
access="property"
column="START_DATE"
not-null="true"
unique="false"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Player.xml
containing the additional properties and place it in your merge dir.
-->

</class>

<query name="findAllPlayers"><![CDATA[
from dk.te.deb.pojo.Player
]]></query>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Timestamp now = new Timestamp(System.currentTimeMillis());
Team team = new Team();
team.setTeamName("Cartoons");
team.setCity("Dugburg");
team.setStartDate(now);
Timestamp later = new Timestamp(System.currentTimeMillis()+999999999);
team.setEndDate(later);

Player player1 = new Player("Anders", "And", now, (float)120000, 100, null, now, later);
Player player2 = new Player("Mickey", "Mouse", now, (float)120000, 100, null, now, later);
Player player3 = new Player("Goofy", "Goof", now, (float)120000, 100, null, now, later);
Player player4 = new Player("Gladstone", "Gander", now, (float)120000, 100, null, now, later);
HashSet set = new HashSet();
set.add(player1);
set.add(player2);
set.add(player3);
set.add(player4);
team.setPlayers(set);

Long id = (Long) session.save(team);
session.flush();
session.connection().commit();
session.close();


Full stack trace of any exception that occurs:

Hibernate: insert into TEAM (CITY, TEAM_NAME, END_DATE, START_DATE, TEAM_ID) values (?, ?, ?, ?, default)
Hibernate: values identity_val_local()
net.sf.hibernate.PropertyValueException: not-null property references a null or transient value: dk.te.deb.pojo.Player.team
at net.sf.hibernate.impl.SessionImpl.checkNullability(SessionImpl.java:1287)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:939)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:868)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:786)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:749)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1398)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:962)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:868)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:786)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:749)
at dk.te.deb.util.Test.main(Test.java:49)
net.sf.hibernate.PropertyValueException: not-null property references a null or transient value: dk.te.deb.pojo.Player.team

Name and version of the database you are using:
DB2 8.2

The generated SQL (show_sql=true):


Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 18, 2005 1:40 pm 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Well - you've marked the relationship as a birectional relationship and haven't done enough to update both ends of the relationship.

try
Code:
Team team = new Team();
Set players = new HashSet();

Player player = new Player(...)
player.setTeam(team);

players.add(player);

team.setPlayers(players)


You could off course simplify this to (a method in Team)
Code:
public void addPlayer(Player toAdd) {
    if (players == null) {
        players = new HashSet();
    }
    toAdd.setTeam(this); // this must happen first
    players.add(toAdd);
}



This is well documented in the section on Parent/Child relationships


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 21, 2005 3:35 am 
Beginner
Beginner

Joined: Tue Feb 15, 2005 8:14 am
Posts: 32
Thanks, I am new to using hibernate, but now it seems to work fine.


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