-->
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.  [ 8 posts ] 
Author Message
 Post subject: <id> tags
PostPosted: Fri May 13, 2005 4:26 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
Im having some trouble understanding the <id> tags in hibernate:

Hibernate version: 2.0

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping
>
<class
name="co.za.ep.CDS2.bus.Customer"
table="customer_tbl"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>

<id
name="id"
column="customer_id"
type="int"
unsaved-value="null"
>
<generator class="native">
</generator>
</id>

<property
name="name"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="customer_name"
/>
</class>

</hibernate-mapping>


Customer cs = new Customer();
cs.setName("Don");
cs.setId(3);
sess.save(cs);




MySql

When I do this I get a null in my customer_id field. Shouldnt it be 3? Why does hibernate want to generate a id if I give it to hibernate? I dont know if I'm understanding this correctly. Could someone please fill this void I have in my hibernate knowledge.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 4:38 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
You are mixing assigned and generated keys. RTM Chapter 6.1.4.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 5:00 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
Hi there,

Thanks, that helped alot. Why would the "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" state that you have to have a <generator > in your <id> ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 5:19 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
use
Code:
generator="assigned"


HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 6:02 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
Thanx, its working nicely. Why do I get this error with the above mapping file?

Code:
org.xml.sax.SAXParseException: Attribute "access" must be declared for element type "property".
[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 7:37 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
How would you go abouts using a class as a id? I have a class called delivery which does not have a id field but it contains a field that is an Order which uniquely identfy it. How would I map such an Id?
Code:
public class Delivery {
   
   private Order __order;
   private ServiceProvider __serviceProvider;

   public void setOrder(Order order) {
      __order = order;
   }      

   public void setTokens(List tokens) {
      __tokens = tokens;
   }

   public Order getOrder() {
      return __order;
   }   

   public List getTokens() {
      return __tokens;
   }   

}

Code:
public class Order{
                private List __orders;
                private String __order_number;

   public List getOrders() {
      return __order;
   }

   public void setOrder(List orders) {
      __order = order;
   }
               
   public String getOrderNumber() {
      return __order_number;
   }

   public void setOrderNumber(String order_number) {
      __order_number = order_number;
   }



}


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 8:44 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
srossouw wrote:
How would you go abouts using a class as a id? I have a class called delivery which does not have a id field but it contains a field that is an Order which uniquely identfy it. How would I map such an Id?
Code:
public class Delivery {
   
   private Order __order;
   private ServiceProvider __serviceProvider;

   public void setOrder(Order order) {
      __order = order;
   }      

   public void setTokens(List tokens) {
      __tokens = tokens;
   }

   public Order getOrder() {
      return __order;
   }   

   public List getTokens() {
      return __tokens;
   }   

}

Code:
public class Order{
                private List __orders;
                private String __order_number;

   public List getOrders() {
      return __order;
   }

   public void setOrder(List orders) {
      __order = order;
   }
               
   public String getOrderNumber() {
      return __order_number;
   }

   public void setOrderNumber(String order_number) {
      __order_number = order_number;
   }



}


Check out the "foreign" id generator.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 16, 2005 3:32 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
Cant seem to get it working, can anyone give me some direction?

this is my mapping file so far:

Code:
<hibernate-mapping
>
    <class
        name="co.za.ep.CDS2.bus.Delivery"
        table="delivery_tbl"
        dynamic-update="false"
        dynamic-insert="false"
        select-before-update="false"
        optimistic-lock="version"
    >

   <id type="int">
           <generator class="foreign">
               <param name="property">order</param>
           </generator>
   </id>





        <bag
            name="order"
            lazy="false"
            inverse="false"
            cascade="none"
        >

              <key
                  column="delivery_id"
              >
              </key>

              <one-to-many
                  class="co.za.ep.CDS2.bus.Order"
              />

        </bag>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Customer.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


And this is the error I get:
Code:
net.sf.hibernate.PropertyAccessException: exception setting property value with CGLIB setter of co.za.ep.CDS2.bus.Delivery.?
   at net.sf.hibernate.persister.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:163)
   at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:1964)
   at net.sf.hibernate.loader.Loader.doFind(Loader.java:196)
   at net.sf.hibernate.loader.Loader.find(Loader.java:604)
   at net.sf.hibernate.hql.QueryTranslator.find(QueryTranslator.java:912)
   at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1356)
   at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1335)
   at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1327)
   at co.za.ep.CDS2.bus.GetTest.main(GetTest.java:39)
Caused by: java.lang.ClassCastException
   at co.za.ep.CDS2.bus.DeliveryMetaClass0.setPropertyValues(<generated>)
   at net.sf.hibernate.persister.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:158)
   ... 8 more
Exception in thread "main"


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