-->
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: Simple Parent/Child relationship IllegalArgumentException
PostPosted: Mon Jun 20, 2005 11:26 am 
Newbie

Joined: Tue Nov 16, 2004 3:54 pm
Posts: 5
Haven't been able to get this simple example to work.

Ideally I want the parent primary key and children primary keys generated by database but save parent/children with one session.save command.

Hibernate version:3.0

Mapping documents:
<!-- Team.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" >

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Wed Jun 15 12:17:48 EDT 2005 -->
<hibernate-mapping package="com.cvs.rx.batch.hibernate">

<class name="Team" table="Team">
<id name="id" column="id" type="java.lang.Long" unsaved-value="null">
<generator class="assigned"/>
</id>

<property name="name" column="name" type="java.lang.String" not-null="true" />

<set name="players" table="player" lazy="true" cascade="save-update" inverse="true" >
<key column="team_id"/>
<one-to-many class="Player"/>
</set>
</class>

</hibernate-mapping>

<!--Player.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" >

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Wed Jun 15 12:17:48 EDT 2005 -->
<hibernate-mapping package="com.cvs.rx.batch.hibernate">


<class name="Player" table="Player">
<id name="id" column="id" type="java.lang.Long" unsaved-value="null">
<generator class="native"/>
</id>

<many-to-one name="teamId" class="Team" column="team_id" cascade="save-update"/>
<property name="firstname" column="firstName" type="java.lang.String" not-null="true" />
<property name="lastname" column="lastName" type="java.lang.String" not-null="true" />
</class>

</hibernate-mapping>


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

Team team = new Team(new Long(1));
team.setName("Pistons");
try {
s = SessionManager.currentSession();
//s.saveOrUpdate(team);
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
Set players = new HashSet();

Player p1 = new Player();
p1.setFirstname("Chucky");
p1.setLastname("Atkins");
p1.setTeam(team);
team.addToplayers(p1);
/* i don't think this is needed but it doesn't work either way */
p1.setTeamId(team.getId());

try {
s.save(team);
s.connection().commit();
s.flush();
} catch (Exception e) {
System.out.println(e.getStackTrace());
}

Full stack trace of any exception that occurs:
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.cvs.rx.batch.hibernate.AbstractTeam.id
java.lang.IllegalArgumentException: object is not an instance of declaring class
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.cvs.rx.batch.hibernate.AbstractTeam.id
org.hibernate.exception.NestableDelegate@b0ede5
IllegalArgumentException occurred calling
class com.cvs.rx.batch.hibernate.AbstractTeam
id
[org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:119), org.hibernate.tuple.AbstractTuplizer.getIdentifier(AbstractTuplizer.java:103), org.hibernate.persister.entity.BasicEntityPersister.getIdentifier(BasicEntityPersister.java:2944), org.hibernate.persister.entity.BasicEntityPersister.isTransient(BasicEntityPersister.java:2705), org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181), org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:409), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:82), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69), org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468), org.hibernate.engine.Cascades$5.cascade(Cascades.java:154), org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:771), org.hibernate.engine.Cascades.cascade(Cascades.java:720), org.hibernate.engine.Cascades.cascade(Cascades.java:847), org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:332), org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:214), org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160), org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:95), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:96), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69), org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468), org.hibernate.engine.Cascades$5.cascade(Cascades.java:154), org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:771), org.hibernate.engine.Cascades.cascade(Cascades.java:720), org.hibernate.engine.Cascades.cascadeCollection(Cascades.java:895), org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:792), org.hibernate.engine.Cascades.cascade(Cascades.java:720), org.hibernate.engine.Cascades.cascade(Cascades.java:847), org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:363), org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:265), org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160), org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184), org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173), org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69), org.hibernate.impl.SessionImpl.save(SessionImpl.java:481), org.hibernate.impl.SessionImpl.save(SessionImpl.java:476), com.cvs.rx.batch.hibernate.TestHB.testTeam(TestHB.java:69), com.cvs.rx.batch.hibernate.TestHB.main(TestHB.java:37)]
false
Name and version of the database you are using:
SQL Server 2000
[


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 20, 2005 11:35 am 
Newbie

Joined: Tue Mar 22, 2005 5:43 pm
Posts: 16
Location: Teksouth
In your table definition in the database, have you defined the column id as primary key and auto-increment?


Top
 Profile  
 
 Post subject: Table definitions
PostPosted: Mon Jun 20, 2005 11:42 am 
Newbie

Joined: Tue Nov 16, 2004 3:54 pm
Posts: 5
Here are my table definitions.
As you can see Team table primary key is assigned by the application.

CREATE TABLE [Team] (
[id] [numeric](19, 0) NOT NULL ,
[name] [varchar] (50) NOT NULL ,
PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY] ,
UNIQUE NONCLUSTERED
(
[name]
) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [Player] (
[id] [numeric](19, 0) IDENTITY (1, 1) NOT NULL ,
[team_id] [numeric](19, 0) NOT NULL ,
[firstName] [varchar] (50) NOT NULL ,
[lastName] [varchar] (50) NOT NULL ,
PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY] ,
CONSTRAINT [FK522A9BD61107FE9C] FOREIGN KEY
(
[team_id]
) REFERENCES [Team] (
[id]
)
) ON [PRIMARY]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 20, 2005 12:19 pm 
Newbie

Joined: Tue Mar 22, 2005 5:43 pm
Posts: 16
Location: Teksouth
team.addToPlayers(p1)

does it look like this

public void addToPlayers(Player p1) {
p1.getTeamId.add(this);
players.add(p1);
}


Top
 Profile  
 
 Post subject: Clarification needed
PostPosted: Mon Jun 20, 2005 1:55 pm 
Newbie

Joined: Tue Nov 16, 2004 3:54 pm
Posts: 5
Here is my method.
public void addToplayers (com.cvs.rx.batch.hibernate.Player player) {
if (null == getPlayers()) setPlayers(new java.util.HashSet());
getPlayers().add(player);
}

If you look at my code in the main file...

Player p1 = new Player();
p1.setFirstname("Chucky");
p1.setLastname("Atkins");
p1.setTeam(team);
team.addToplayers(p1);
p1.setTeamId(team.getId());

As you can see I do set the P1's team to the new team instance.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 20, 2005 2:55 pm 
Newbie

Joined: Tue Mar 22, 2005 5:43 pm
Posts: 16
Location: Teksouth
You can not use id that way, because the id is not allocated until saved. So in your player teamid (which is a team record anyway) add it the way I have shown in my previous post and try. If you want to do it the you have done, then you must save the team record. Then do session.refresh(team) and then get the id from team record.


Top
 Profile  
 
 Post subject: Problem solved
PostPosted: Tue Jun 21, 2005 10:14 am 
Newbie

Joined: Tue Nov 16, 2004 3:54 pm
Posts: 5
I was able to solve this problem.

This is the snippet of my code...

Team team = new Team();
team.setName("Pistons");
Set set = new HashSet();
Player p1 = new Player();
p1.setFirstname("Chucky");
p1.setLastname("Atkins");
p1.setTeamId(team);
set.add(p1);
Player p2 = new Player();
p2.setFirstname("AChucky2");
p2.setLastname("BAtkins2");
p2.setTeamId(team);
set.add(p2);
team.setPlayers(set);
try {
s = SessionManager.currentSession();
s.saveOrUpdate(team);
} catch (Exception e) {
System.out.println(e.getStackTrace());
}

try {
s.connection().commit();
s.flush();
s.close();
} catch (Exception e) {
System.out.println(e.getStackTrace());
System.out.println(e.getMessage);
==========================================
Config files
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Wed Jun 15 12:17:48 EDT 2005 -->
<hibernate-mapping package="com.cvs.rx.batch.hibernate">


<class name="Player" table="Player">
<id name="id" column="id" type="java.lang.Long">
<generator class="native"/>
</id>

<many-to-one name="teamId" class="com.cvs.rx.batch.hibernate.Team" column="team_id" not-null="true"
/>
<property name="firstname" column="firstName" type="java.lang.String" not-null="true" />
<property name="lastname" column="lastName" type="java.lang.String" not-null="true" />
</class>

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

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Wed Jun 15 12:17:48 EDT 2005 -->
<hibernate-mapping package="com.cvs.rx.batch.hibernate">

<class name="Team" table="Team">
<id name="id" column="id" type="java.lang.Long">
<generator class="select"/>
</id>

<property name="name" column="name" type="java.lang.String" not-null="true" />

<set name="players" table="player" cascade="persist,merge,save-update" inverse="true" >
<key column="team_id"/>
<one-to-many class="com.cvs.rx.batch.hibernate.Player"/>
</set>
</class>

</hibernate-mapping>
======================================
If anybody needs any more info reply to this topic.


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.