Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override hibernate collection join column?

Tags:

java

hibernate

This is my abstract father:

@MappedSuperclass   
public abstract class AbstractEntity implements Serializable {

    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "entity_no", referencedColumnName = "MY_COLUMN")
    private Set<CLASS_TYPE> list; 
}

All my entities has this set, but each entity has different referencedColumnName.

Is there a way to override only the @JoinColumn annotation?

like image 453
Ido Barash Avatar asked Oct 23 '12 09:10

Ido Barash


2 Answers

You can use AssociationOverride annotation. In your case it would look like this:

@Entity
@AssociationOverrides({
   @AssociationOverride(name = "list",
      joinColumns = @JoinColumn(referencedColumnName = "COLUMN_NEW_NAME"))
})
public class ConcreteEntity extends AbstractEntity {

}
like image 65
kaos Avatar answered Nov 07 '22 21:11

kaos


If you define annotations on the properties (i.e. getters) instead of on the fields, then you can override the public Set<CLASS_TYPE> getList() in each of the sub classes and define the @JoinColumn separately.

like image 21
Vikdor Avatar answered Nov 07 '22 20:11

Vikdor