Hibernate declare composite primary key in JoinTable (List<>)
composite-primary-key, hibernate, java, pie-chart
Solution
Answers / solutions:
Change the List<> to a Set<> will do all the magic in this situation.
You can create the table manualy using HQL.
- If you ensist on using List I only know a way to declare the(single) primary key using "columnDefinition" and you could set both columns to be unique together by setting the unique constraint with the "@UniqueConstraint" annotation like this:
@ManyToMany
@JoinTable(
name="tbl_settings_objectproxy_for_something",
joinColumns = @JoinColumn(name = "id", columnDefinition = "int primary key"),
inverseJoinColumns = @JoinColumn( name = "objectproxy_id")
uniqueConstraints = {@UniqueConstraint(columnNames={"id", "objectproxy_id"})}
)
private List<SomeObject> SomeObjectProxy;
vote up the comment of JB Nizet because those are his solutions, but he gave them in a comment instead of an answer.
Problem
Hibernate declare composite primary key in JoinTable (List<>) How do I declare the composite primary key in a side table using HQL (Hibernate query language)? Previously I got one jointable declared in my class and everything worked fine, it created a composite primary key of the two columns. This is the code I used: BEFORE (worked) ``` @Id @SequenceGenerator(name="someSequence", sequenceName="SEQ_APP", allocationSize =1) @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="appSequence") @Column(name="id") private int setting_id; @OneToOne private User user; @ManyToMany @JoinTable( name="tbl_settings_objectproxy", joinColumns = @JoinColumn(name = "id"), inverseJoinColumns = @JoinColumn( name = "objectproxy_id") ) private Set<SomeObject> objectproxy; ``` Now I scrued everything up, I changed the Set to a List and added another side table. Now hibernate creates two side tables as it is supposed to do but it doesn't declare any primary key's... does anyone know how to fix this? This is my new code: AFTER (it doesn't create composite primary key anymore, it doesn't even declare any primary key) ``` @Id @SequenceGenerator(name="someSequence", sequenceName="SEQ_APP", allocationSize =1) @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="appSequence") @Column(name="id") private int setting_id; @OneToOne private User user; @ManyToMany @JoinTable( name="tbl_settings_objectproxy", joinColumns = @JoinColumn(name = "id"), inverseJoinColumns = @JoinColumn( name = "objectproxy_id") ) private List<SomeObject> objectProxyForSomething; ```