-->
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: how to save objects that contain other saved objects
PostPosted: Thu Oct 20, 2005 6:39 am 
Newbie

Joined: Fri Sep 16, 2005 6:35 am
Posts: 2
hi,
I am having some problem saving an object that contains an object that is already saved in the database. e.g. I have a class A that has a Code. These codes are entered earlier and whenever an object of type A is made, an object of type Code is assigned to it.

@Entity(access=AccessType.FIELD)
public class A

{

@OneToOne(fetch=FetchType.EAGER, cascade={CascadeType.MERGE, CascadeType.PERSIST})})
@JoinColumn(name="CodeID")
private Code code;


public void setCode(Code c)
{
this.code = c;
}

public Code getCode()
{
return this.code ;
}

}

@Entity(access=AccessType.FIELD)
public class Code
{
String description;
String codeNumber;


}

The problem is that when i save object of class A, it gives me an exception of duplicate entry for the object of code

what am i doing wrong?? kindly reply asap if you know the solution..


Top
 Profile  
 
 Post subject: id
PostPosted: Sun Oct 23, 2005 11:04 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
What is the identifier for code and how H is supposed to know if it needs to save/update/do-nothing with your object.

I do not see something like this
@Id(generate=GeneratorType.IDENTITY)
public Long getId() { ... }

Or perhaps for Code it should be something like:

@Id( generate=GeneratorType.NONE)
String getCodeNumber()

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 12:16 am 
Newbie

Joined: Fri Sep 16, 2005 6:35 am
Posts: 2
yeah, the id for both classes are generated using the AUTO strategy.. i forgot to add it here.. here is the modified code.. i am still having the same problem... if any one knows the answer kindly let me know..

@Entity(access=AccessType.FIELD)
public class A

{

@Id(generate=GeneratorType.AUTO)
private Integer id;


@OneToOne(fetch=FetchType.EAGER, cascade={CascadeType.MERGE, CascadeType.PERSIST})})
@JoinColumn(name="CodeID")
private Code code;

public Integer getId()
{
return id;
}

public void setCode(Code c)
{
this.code = c;
}

public Code getCode()
{
return this.code ;
}

}

@Entity(access=AccessType.FIELD)
public class Code
{
String description;
String codeNumber;

@Id(generate=GeneratorType.AUTO)
private Integer id;


public Integer getId()
{
return id;
}

}


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.