Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:3.2.1.ga
Mapping documents:orm.xml and persistence.xml
Using Spring's JpaTemplate
Full stack trace of any exception that occurs: None
Name and version of the database you are using:MYSQL 5.0.45-community-nt
Hello
I have a problem when using one-to-many-relations where i have cascade-persist and cascade-merge
Mapping:
Code:
<entity class="domain.Incident" name="Incident">
<!-- id and version is in a mapped superclass -->
<attributes>
<basic name="sensorId" />
<one-to-many name="receivedMessages">
<cascade>
<cascade-persist />
<cascade-merge />
</cascade>
</one-to-many>
</attributes>
</entity>
<entity class="domain.ReceivedSMS" name="ReceivedSMS">
<!-- id and version is in a mapped superclass -->
<attributes>
<basic name="senderCellphoneNumber" />
<basic name="messageText" />
</attributes>
</entity>
Domain objects:
Code:
public class Incident extends AbstractDomainObject {
private Long sensorId;
private Set<ReceivedSMS> receivedMessages = new
HashSet<ReceivedSMS>()
public void setSensorId(Long sensorId){
this.sensorId = sensorId;
}
public Long getSensorId(){
return sensorId;
}
public void setReceivedMessages(Set<ReceivedSMS> messages){
this.receivedMessages = messages;
}
public Set<ReceivedSMS> getReceivedMessages(){
return receivedMessages;
}
// This is where the problem is
public void addReceivedMessage(ReceivedSMS sms){
receivedMessages.add(sms);
}
}
Repository code for saving incidents:
Code:
public class RepositoryIml extends JpaTemplate implements Repository {
public Incident save(Incident incident){
if (incident.getId == null){
persist(incident);
} else {
incident = merge(incident);
}
return incident;
}
...
}
The ReceivedSMS instances are saved before they are added to the incident.
When i create a new Incident, and add a ReceivedSMS to it (before saving), everything works ok.
But when trying to add a ReceivedSMS to an already saved Incident, the ReceivedSMS is not bound to the incident in the relation table.
Do i do something wrong or is it a bug?
I'm quite new to Hibernate
/Per-Jarle