-->
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: howto define PersistantObject superclass
PostPosted: Tue Jan 03, 2006 5:32 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
I 'd like to define a PersistantObject superclass and same myself a lot of typing on every subclass by defining a superclass mapping like this:

Code:
    <class name="x.y.PersistableObject" abstract="true">
        <id name="databaseId" type="java.lang.Long">
            <generator class="native"/>
        </id>
        <version name="version" type="long"/>
    </class>

And a subclass mapping like this:
Code:
    <union-subclass name="x.y.Dog"
            extends="x.y.PersistableObject">

        <property name="name"/>
        <!-- ... -->
    </union-subclass>


But apperently Hibernate doesn't support this: "native generator inhertance"?

How can I avoid having to write the id and version tag in every file again?

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 03, 2006 7:25 am 
Regular
Regular

Joined: Wed May 05, 2004 8:01 am
Posts: 53
If you use XDoclet and have your hbm.xml files generated you get your requirement fulfilled with no additional tasks:

Code:
public abstract class Persistent {
   private Long   id;

   /**
    * @hibernate.id generator-class="native"
    */
   public Long getId() {
      return id;
   }

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

public class Product extends Persistent {
   private String         name;

   /**
    * @clientRole belongs to
    */
   private Contractor      contractor;

   /**
    * @clientCardinality 0..*
    * @supplierCardinality 1
    */
   private ProductGroup   group;
   private String         eanNumber;
   private String         producerNumber;
   private DictionaryValue   wrapping;
   private Integer         wrappingQuantity;
   private String         basisWeight;

   public Product() {
   }

   public Product( String name ) {
      this.name = name;
   }

   /**
    * @hibernate.property not-null="true"
    */
   public String getName() {
      return name;
   }

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

   /**
    * @hibernate.many-to-one not-null="true"
    */
   public Contractor getContractor() {
      return contractor;
   }

   public void setContractor( Contractor contractor ) {
      this.contractor = contractor;
   }

   /**
    * @hibernate.many-to-one not-null="true" column="productGroup"
    */
   public ProductGroup getGroup() {
      return group;
   }

   public void setGroup( ProductGroup group ) {
      this.group = group;
   }

   /**
    * @hibernate.property not-null="false"
    */
   public String getEanNumber() {
      return eanNumber;
   }

   public void setEanNumber( String eanNumber ) {
      this.eanNumber = eanNumber;
   }

   /**
    * @hibernate.property not-null="false"
    */
   public String getProducerNumber() {
      return producerNumber;
   }

   public void setProducerNumber( String producerNumber ) {
      this.producerNumber = producerNumber;
   }

   /**
    * @hibernate.many-to-one not-null="false"
    */
   public DictionaryValue getWrapping() {
      return wrapping;
   }

   public void setWrapping( DictionaryValue wrapping ) {
      this.wrapping = wrapping;
   }

   /**
    * @hibernate.property not-null="false"
    */
   public String getBasisWeight() {
      return basisWeight;
   }

   public void setBasisWeight( String basisWeight ) {
      this.basisWeight = basisWeight;
   }

   /**
    * @hibernate.property not-null="false"
    */
   public Integer getWrappingQuantity() {
      return wrappingQuantity;
   }

   public void setWrappingQuantity( Integer wrappingQuantity ) {
      this.wrappingQuantity = wrappingQuantity;
   }
}


this will generate:
Code:
<hibernate-mapping
>
    <class
        name="com.mobilebox.pmt.model.Product"
    >

        <id
            name="id"
            column="id"
            type="java.lang.Long"
        >
            <generator class="native">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-Product.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
            not-null="true"
        />

        <many-to-one
            name="contractor"
            class="model.Contractor"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="contractor"
            not-null="true"
        />

        <many-to-one
            name="group"
            class="model.ProductGroup"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="productGroup"
            not-null="true"
        />

        <property
            name="eanNumber"
            type="java.lang.String"
            update="true"
            insert="true"
            column="eanNumber"
            not-null="false"
        />

        <property
            name="producerNumber"
            type="java.lang.String"
            update="true"
            insert="true"
            column="producerNumber"
            not-null="false"
        />

        <many-to-one
            name="wrapping"
            class="model.DictionaryValue"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="wrapping"
            not-null="false"
        />

        <property
            name="basisWeight"
            type="java.lang.String"
            update="true"
            insert="true"
            column="basisWeight"
            not-null="false"
        />

        <property
            name="wrappingQuantity"
            type="java.lang.Integer"
            update="true"
            insert="true"
            column="wrappingQuantity"
            not-null="false"
        />

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

    </class>

</hibernate-mapping>


if you switch to Java 5.0 annotations you just should use @Entity and @EmbeddableSuperclass and that's all.

Did that help?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 03, 2006 8:30 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
Thanks, that should do it :)
I was going to switch to tiger annotations anyway.

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 06, 2006 10:57 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
I just switched to tiger annotations, but this didn't solve it.

I got the exception:
Caused by: org.hibernate.MappingException: Cannot use identity column key generation with <union-subclass> mapping for: x.PersistableObject

for these code:

Code:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class PersistableObject implements Comparable<PersistableObject>, Serializable {

    private Long id;

    public PersistableObject() {
    }

    @Id(generate = GeneratorType.AUTO)
    public Long getId() {
        return id;
    }

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


Code:
@Entity
public class User extends PersistableObject implements UserDetails {
...
}

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 06, 2006 11:06 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
Making PersistableObject @EmbeddableSuperclass instead of @Entity fixes my problem

_________________
http://www.ohloh.net/accounts/ge0ffrey


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.