2

I want to extend a class with hibernates joined-subclass.

The problem is, that the extended class had a composite-key and the normal class only has a normal primary key (see image). enter image description here

Is there a option to realize this class-structure?

Currently the hbm.xml looks so:

<?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="Product" table="products" catalog="test">
     <id name="idProduct" type="java.lang.Integer">
        <column name="idProduct" />
        <generator class="identity" />
    </id>
    <property name="nameProduct" type="string">
        <column name="name_Product" length="45" />
    </property>
    <property name="descriptionProduct" type="string">
        <column name="description_Product" length="45" />
    </property>
    <property name="price1Product" type="string">
        <column name="price1_Product" length="45" />
    </property>
    <property name="price2Product" type="string">
        <column name="price2_Product" length="45" />
    </property> 
     <joined-subclass name="ProductWithSelectedPrice" extends="Product" table="category_has_product">
      <key>
        <column name="Category_idCategory" />
        <column name="Product_idProduct" />
      </key>
            <property name="price" type="java.lang.Integer" column="Product_selectedPrice" />
    </joined-subclass>
     -->
</class>

best regards,

Christian St.
  • 1,751
  • 2
  • 22
  • 41
501 - not implemented
  • 2,638
  • 4
  • 39
  • 74

1 Answers1

0

First of all it is a de facto i think in Java OOP to FAVOR COMPOSITION RATHER THAN INHERITANCE and as you can see its not a good practice to just extend classes in hibernate.

As i understand you want to implement a Many to Many relationship you could just do it like these:

@Table("Category")
public class Category implements Serializable {

   long id;
   ...
   ...

   @OneToMany(mappedBy = "category", cascade = { CascadeType.ALL }, orphanRemoval = true, fetch = FetchType.LAZY)
   List<Category_has_Product> category_has_Product;


   public List<Category_has_Product> getCategory_has_Product(){
      return this.category_has_Product
   }
}
@Table("Product")
public class Product implements Serializable {

   long id;
   ...
   ...

   @OneToMany(mappedBy = "product", cascade = { CascadeType.ALL }, orphanRemoval = true, fetch = FetchType.LAZY)
   List<Category_has_Product> category_has_Product;
}

public class Category_has_Product implements Serializable {

  @ManyToOne(optional = false)
  Category category;

  @ManyToOne(optional = false)
  Product product;

  public Product getProduct() {
         return this.product;
  }
}

and so now you could just do it simple like these

Category category = new CategoryService().findById(101L);
List<Category_has_Product> category_has_Product  = category.getCategory_has_Product();

and then

Product product = category_has_Product.get(0).getProduct();

i hope it helps :D

  • Hi, in time I've solved the problem but currently I don't have time to answer this here. But jeah. My solution looks near similar to yours. The idea is from the`CategorizedItem` example from `Java - Persistence with Hibernate` (Listing 7.1) – 501 - not implemented Jul 15 '14 at 06:37
  • Sure that is.. just anotate right and hibernate will do the rest :) – Mark Anthony Ortiz Jul 15 '14 at 06:41