-->
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.  [ 5 posts ] 
Author Message
 Post subject: can not saveorupdate please help
PostPosted: Thu Feb 18, 2010 8:48 am 
Newbie

Joined: Thu Feb 18, 2010 8:35 am
Posts: 3
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]


Top
 Profile  
 
 Post subject: Re: can not saveorupdate please help
PostPosted: Thu Feb 18, 2010 9:25 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:02 am
Posts: 48
You run it first time, hibernate checks the id field of author object and it is not set.
So hibernate figures out it is a new record so assigns an author Id to the author and saves it to the database.

second time you run it, hibernate again checks id field of author object and it is not set.
So hibernate again assumes it is a new record so assigns another author Id to the author and tries to save a second record to the database with the same email address as before. since email is unique , you get the exception.

What are you trying to do when you run it second time?


Top
 Profile  
 
 Post subject: Re: can not saveorupdate please help
PostPosted: Thu Feb 18, 2010 9:26 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Problem is : it works only once and from next time onwards it is giving
error for duplicate entry.


If you run your code two times you have created a new Author() two times. Both with the same email, and since you have mapped the email column with unique="true" the error is expected. What do you want the code to do the second time? Update the author that was created the first time? Create a second author?

Note that the saveOrUpdate() method simply works by comparing the id of the entity with the unsaved-value attribute in your mapping. In your case... if id=0 call save(), otherwise call update().


Top
 Profile  
 
 Post subject: Re: can not saveorupdate please help
PostPosted: Thu Feb 18, 2010 9:30 am 
Newbie

Joined: Thu Feb 18, 2010 8:35 am
Posts: 3
Thanks for your answers,

What I am trying to accomplish is that, the second time, if the email id is different a row with the new author details and the posted message will be saved, if the email id already exists (existing author).. it will simply create a row in the Message but wouldn't create an author..

I could get around this problem by using an HQL to check if author with this email id exists, but is it possible to get it checked automatically and save/update... without using HQL.


Top
 Profile  
 
 Post subject: Re: can not saveorupdate please help
PostPosted: Thu Feb 18, 2010 9:44 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You'll have to find out if there is any author with a given id using HQL, a named query, critera, or something else. The saveOrUpdate() method only checks the id against the unsaved-value attribute. You could map the email property with <natural-id> (see http://docs.jboss.org/hibernate/stable/ ... -naturalid). It will not affect anything now, but I think the Hibernate developers are planning to implement loading by natural id for the 3.6 release (see http://opensource.atlassian.com/project ... e/HHH-2879).


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