Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate many-to-one foreign key annotation mapping
    text
    copied!<p>I use Hibernate 4 with Spring 3.1 and I have a problem with foreign key constraints which are generated from this mapping.</p> <pre><code>@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class O { private String oid; @Id @GeneratedValue(generator = "OidGenerator") @GenericGenerator(name = "OidGenerator", strategy = "com.evolveum.midpoint.repo.sql.OidGenerator") @Column(unique = true, nullable = false, updatable = false, length = 36) public String getOid() { return oid; } ...other getters and setters } @Entity public class Role extends O { private Set&lt;Assignment&gt; assignments; @OneToMany(mappedBy = "owner") @Cascade({org.hibernate.annotations.CascadeType.ALL}) public Set&lt;Assignment&gt; getAssignments() { return assignments; } ...other getters and setters } @Entity public class User extends O { private Set&lt;Assignment&gt; assignments; @OneToMany(mappedBy = "owner") @Cascade({org.hibernate.annotations.CascadeType.ALL}) public Set&lt;Assignment&gt; getAssignments() { return assignments; } ...other getters and setters } @Entity public class Assignment extends IdentifiableContainer { private O owner; private Long id; @Id @MapsId("oid") @ManyToOne public O getOwner() { return owner; } @Id @GeneratedValue(generator = "ContainerIdGenerator") @GenericGenerator(name = "ContainerIdGenerator", strategy = "com.evolveum.midpoint.repo.sql.ContainerIdGenerator") @Column(nullable = true, updatable = true) public Long getId() { return id; } ...other getters and setters } </code></pre> <p>this produces schema like this...</p> <pre><code>create table Assignment ( id bigint not null, owner_oid varchar(36) not null, primary key (owner_oid, id) ); create table Role ( oid varchar(36) not null unique, primary key (oid) ); create table User ( oid varchar(36) not null unique, primary key (oid) ); alter table Assignment add constraint FKB3FD62ED72781986 foreign key (owner_oid) references User; alter table Assignment add constraint FKB3FD62ED7276AE31 foreign key (owner_oid) references Role; </code></pre> <p>that two constraints on Assignment.owner_oid should not be there. I can't save role/user with assignments because of them. I thought about other solution with join tables, but I looks too akward. Join table would look like</p> <pre><code>create table Role_Assignment ( role_oid varchar(36) ... assignment_oid varchar(36) ... assignment_id bigint ... primary key (....) ); </code></pre> <p>but I have assignment.owner_oid annotated with @MapsId therefore in this join table first two colums would be always the same. Also I have couple more classes that extends from O.class, which means many join tables. How to disable that FK constraints on assignment table?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload