1

I have this UML diagram.

enter image description here

And I tried to build entities like this (I renamed Entity class to Entidad)

RelationshipType.java

@Entity
@Table(name = "relationship_type")
public class RelationshipType {

    @Id
    @GeneratedValue
    private Long id;

    private String type;

    @OneToMany(mappedBy = "relationshipType", fetch = FetchType.EAGER)
    private Set<Relationship> relationships = new HashSet<Relationship>();
    //Getters and Setters

Relationship.java

@Entity
@Table(name = "relationship")
public class Relationship {

    @Id
    @ManyToOne
    private RelationshipType relationshipType;

    @Id
    @ManyToOne
    private Entidad entity;
    //Getters and Setters

Entidad.java

@Entity
@Table(name = "entity")
public class Entidad {
    @Id
    @GeneratedValue
    private Long id;

    private String image;

    private String foundationNotes;

    private String alias;

    private Boolean excludeNotifications;

    private String notes;

    //[...]

    @ManyToOne 
    private Relationship related;

    @OneToMany(mappedBy = "entity", fetch = FetchType.EAGER)
    private Set<Relationship> relationships = new HashSet<Relationship>();

But when I launch app throws this:

Foreign key (FK_9d8afoh1pv9r59iwjkbcpnud1:entity [])) must have same number of columns as the referenced primary key (relationship [relationshipType_id,entity_id])

At now, I don't know where is the problem and need do this well because I'm using this entities to build the DB schema.

bubi
  • 6,414
  • 3
  • 28
  • 45
Kratul
  • 158
  • 2
  • 13
  • http://stackoverflow.com/questions/3585034/how-to-map-a-composite-key-with-hibernate – Stefan Aug 18 '15 at 08:02
  • I think I'ts a different case, in this question I have 2 calls from Relationship in the same entity – Kratul Aug 18 '15 at 08:10
  • 1
    can you comment out these lines one by one and confirm if the error is because of Many to One : @ManyToOne private Relationship related; @OneToMany(mappedBy = "entity", fetch = FetchType.EAGER) private Set relationships = new HashSet(); –  Aug 18 '15 at 08:15
  • @amitmahajan yes, if I comment `@ManyToOne private Relationship related;` and implements Serializable in RelationshipType it works, but I think i need this field, but I'm not sure (I never use a UML diagram...) – Kratul Aug 18 '15 at 08:23
  • I am not using hibernate anymore for long time now. As i understand the problem is because hibernate is not able to relate entity to relationship. You may need to specify a column attribute on the ManyToOne annotation. –  Aug 18 '15 at 08:27
  • When you want to use multiple columns for the primary key you need a composite key in hibernate. Unless you do it so, the foreign key from the relation table couldn't reference multi-column primary key of the relationship type. – Roman C Aug 18 '15 at 10:31

0 Answers0