Hi all,
I have this following mapping files.
Author.hbm.xml
Code:
[color=#000000]<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class
name="comm.models.Author"
table="AUTHOR">
<id
name="id"
column="AUTHOR_ID" unsaved-value="0">
<generator class="increment"/>
</id>
<property
name="name"
column="AUTHOR_NAME" not-null="true"/>
<property
name="nick"
column="AUTHOR_NICK" not-null="true"/>
<property
name="password"
column="AUTHOR_PASSWORD" not-null="true"/>
<property
name="email"
column="AUTHOR_EMAIL" not-null="true" unique="true"/>
<property
name="address"
column="AUTHOR_ADDRESS"/>
<property
name="selftext"
column="AUTHOR_SELFTEXT"/>
<property
name="dateOfBirth"
column="AUTHOR_DATEOFBIRTH" type="date" not-null="true"/>
<property
name="dateOfJoin"
column="AUTHOR_DATEOFJOIN" type="date" not-null="true"/>
<set name="messages" table="MESSAGES" lazy="true" cascade="save-update">
<key column="AUTHOR_ID"/>
<one-to-many class="comm.models.Message"/>
</set>
</class>
</hibernate-mapping>[/color]
Message.hbm.xml
Code:
[color=#000000]<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class
name="comm.models.Message"
table="MESSAGES">
<id
name="id"
column="MESSAGE_ID" unsaved-value="0">
<generator class="increment"/>
</id>
<property
name="text"
column="MESSAGE_TEXT"/>
<many-to-one name="messageAuthor" column="AUTHOR_ID"/>
</class>
</hibernate-mapping>[/color]
This is the code that I used for saving author and associated message.
Code:
[color=#000000] Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Author myAuthor = new Author();
myAuthor.setName("Supratim");
myAuthor.setDateOfBirth(new Date());
myAuthor.setEmail("testmail@testmail.com");
myAuthor.setDateOfJoin(new Date());
myAuthor.setNick("sup");
myAuthor.setPassword("testpass");
session.saveOrUpdate(myAuthor);
Message message = new Message();
message.setText("Hello World");
message.setMessageAuthor(myAuthor);
session.saveOrUpdate(message);
tx.commit();
session.flush();
session.close();[/color]
Problem is : it works only once and from next time onwards it is giving
error for duplicate entry.
Code:
[color=#BF0000]SEVERE: Duplicate entry 'testmail@testmail.com' for key 'AUTHOR_EMAIL'[/color]
How can I make this code to run successfully?
Thanks in advance!
Here are the Classes in use.
Author.java
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
[color=#000000]package comm.models;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;
/**
*
* @author supratim
*/
public class Author implements Serializable {
private Long id;
private String name;
private String nick;
private String password;
private String email;
private String address;
private String selftext;
private Date dateOfBirth;
private Date dateOfJoin;
private Set<Message> messages;
public Author() {
}
public Set getMessages() {
return messages;
}
public void setMessages(Set messages) {
this.messages = messages;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public Date getDateOfJoin() {
return dateOfJoin;
}
public void setDateOfJoin(Date dateOfJoin) {
this.dateOfJoin = dateOfJoin;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSelftext() {
return selftext;
}
public void setSelftext(String selftext) {
this.selftext = selftext;
}
}[/color]
Message.java
Code:
[color=#000000]/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package comm.models;
import java.io.Serializable;
/**
*
* @author supratim
*/
public class Message implements Serializable {
private Long id;
private String text;
private Author messageAuthor;
public Message() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Author getMessageAuthor() {
return messageAuthor;
}
public void setMessageAuthor(Author messageAuthor) {
this.messageAuthor = messageAuthor;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
[/color]