Hibernate oneToMany Abstract class override identifier

I am using JPA 1.0, so I am limited in what I can do, but I still think it should be possible to do the following, but I cannot get it to work ...

Table CustomerA a_id Table ProductB a_id b_id Table ProductC a_id c_id @Entity @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) public abstract class AbstractProduct { @Id @GeneratedValue.... private Long id; private String name; @ManyToOne() JoinColumn(name="a_id") private CustomerA customerA; } 

Now I want to create a subclass that can move around Id or create a composite key based on PK of Table A and the key of the derived table ...

 @Entity @Table(name="ProductB") public class ProductB extends AbstractProduct { //@AttributeOverride(name="id", column=@Column (name="B_ID") //Can only be used with MappedSuperClass and also Emmbedded Objects //@Id //cant override the ID Column so that cant go here //PrimaryKeycolumn join not what i want here private Long productB_id; private String productName; } @Entity @Table(name="CustomerA") public class CustomerA { @Id @GeneratedValue.... @Column(name="a_id") private Long aId @OneToMany(mappedBy="customerA", cascade=CascadeType.ALL) private Set<AbstractProduct> product; } 

Thus, essentially CustomerA can contain many products, but there will always be only ProductB or ProductC . How can I override Id in a subclass, since you cannot use attributeoverride and Entity, and if you use @Entity , you must specify @Id whenever you specify @Entity . I read the jpa wiki and it looks rather complicated and not suitable for this in JPA 1.0, but I wonder if I am missing something?

+4
source share
1 answer

My suggestion is to split the base class between your two products so that you can refer to them one at a time. In JPA / Hibernate, this means creating a common table that will contain the common elements of your product, namely a link to CustomerA.

You can accomplish this with the JOINED inheritance strategy:

 @Inheritance(strategy=InheritanceType.JOINED) 

The disadvantage of using this strategy is that it may run into your existing TABLE_PER_CLASS category already used in the AbstractProduct base class .... although it may be the appropriate base class to use for this. From what I remember, you cannot mix inheritance strategies.

Here is an example:

 @Entity @Table(name="CustomerA") public class CustomerA { @Id @GeneratedValue.... @Column(name="a_id") private Long aId @OneToMany(mappedBy="customerA", cascade=CascadeType.ALL) private Set<AbstractProduct> product; } @Entity @Table(name="ProductB") public class ProductB extends AbstractProduct { private String specificProductBValue; } @Entity @Table(name="ProductC") public class ProductC extends AbstractProduct { private String specificProductCValue; } @Entity @Inheritance(strategy=InheritanceType.JOINED) public abstract class AbstractProduct { @Id @GeneratedValue.... private Long id; private String name; @ManyToOne() JoinColumn(name="customer_id") private CustomerA customerA; } 

It looks like you're almost on your own example, you just need to use the right inheritance strategy.

In addition, this assumes that you do not have other products that extend the AbstractProduct base class. If you do this and you don’t need the ones your client refers to, you will have to rethink your domain design.

Hope this helps.

0
source

Source: https://habr.com/ru/post/1382581/


All Articles