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.  [ 4 posts ] 
Author Message
 Post subject: Avoid Col N and M have duplicated value
PostPosted: Tue Oct 02, 2007 5:56 am 
Newbie

Joined: Tue Oct 02, 2007 5:38 am
Posts: 2
Hello all, I have the data class as follow :

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="test.Quotation" table="QUOTATION">
        <id name="id" column="QUOTATION_ID">
            <generator class="native"/>
        </id>
        <property name="name"/>
        <property name="price"/>
    </class>
</hibernate-mapping>


public class Quotation {
   
    /** Creates a new instance of Quotation */
    public Quotation() {
        setName("noname");       
        setPrice(0.0);       
    }

    public Quotation(String name, double price) {
        setName(name);       
        setPrice(price);               
    }
   
    private Long id;
    private double price;
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}



How can I explicitly specific in Hibernate, so that when "name" AND "price" col are having the same value, that particular row will not be saved?

For example,

Code:

        Quotation q1 = new Quotation("sugar", 100);
        Quotation q2 = new Quotation("sugar", 200);
        Quotation q3 = new Quotation("sugar", 100);
       
        session.save(q1);
        session.save(q2);
        session.save(q3);

         | ID | NAME | PRICE
------------------------------------------
         | 1  | sugar  | 100
         | 2  | sugar  | 200
         | 3  | sugar  | 100  <-- I wish this col will not be saved, since it is having same name and price as the first col.




thank you very much


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 02, 2007 8:01 am 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
Hi!

One way, is to have the columns you want as composite id, so that the values are unique.

http://www.hibernate.org/hib_docs/v3/re ... ompositeid

Other way, is to handle the creation and save of the objects differently. So that you will create some DAO pattern that will handle saving of such objects. It will first try to retrieve an object with same values from db, than merge it and save it.

Actually, sometimes it is really helpful to put just another layer on top of your persistent layer (like DAO over hibernate), so that you can do such customized things. However, using this approach will not work when the entities are saved on cascade or so.

Maybe there is also some hibernate way to do this, but I don't it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 03, 2007 12:43 pm 
Newbie

Joined: Tue Oct 02, 2007 5:38 am
Posts: 2
OK. I do a slight modification on my class and xml as follow :

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="test.Quotation" table="QUOTATION">
        <id name="id" column="QUOTATION_ID">
            <generator class="native"/>
        </id>
        <composite-id>
            <key-property name="name" column="NAME"/>
            <key-property name="price" column="PRICE"/>           
        </composite-id>
    </class>
</hibernate-mapping>

package test;

/**
*
* @author YC Cheok
*/
public class Quotation implements java.io.Serializable {
   
    /** Creates a new instance of Quotation */
    public Quotation() {
        setName("noname");       
        setPrice(0.0);       
    }

    public Quotation(String name, double price) {
        setName(name);       
        setPrice(price);               
    }
   
    public boolean equals(Object o) {
        if(!(o instanceof Quotation))
            return false;
       
        Quotation quotation = (Quotation)o;
       
        return this.price == quotation.price && this.name.equals(quotation.name);
    }
   
    public int hashCode() {
        int result = 37;

        final long tmp = Double.doubleToLongBits(price);
        result = 37 * (int)(tmp ^ (tmp >>> 32));

        result = 37 * result + name.hashCode();
       
        return result;
    }
   
    private Long id;
    private double price;
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


However, I get the following error during run-time :

Code:
...
Caused by: org.xml.sax.SAXParseException: The content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,resultset*,(query|sql-query)*)".


Any suggestion? Is there any wrong with my XML file?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 04, 2007 3:21 am 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
I would say you cannot have ID and Composite-ID.
Composite-ID replaces simple ID.

Just remove the ID col. Or don't mark is as ID.

Pavol


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