Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to model a friendship relationship in hibernate?

I need to have a friendship relationship. I have a friendship class with two primary keys that each is a Member class. I am receiving following exception:

org.hibernate.MappingException: Foreign key (FK_8ynretl1yt1xe3gcvfytrvpq:Friendship [])) must have same number of columns as the referenced primary key (Member [username])

Friendship

@Entity
public class Friendship implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -1234656876554786549L;
    @Id
    @ManyToOne
    Member requester;
    @Id
    @ManyToOne
    Member friend;
    @Temporal(javax.persistence.TemporalType.DATE)
Date date;

Member

@Entity
public class Member {
    @Id
    @MapsId
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "username")
    Credential credential;
    @Column(nullable = false)
    String fname;
    @Column(nullable = false)
    String lname;
    @Column(nullable = false)
    short gender;

Credential

@Entity
public class Credential {
    @Id
    @Column(nullable = false, unique = true)
    private String username;
    @Column(nullable = false)
    private String password;
    @Column(nullable = false)
    private String authority;
    @Column(nullable = false)
    private boolean enabled;
like image 726
Jack Avatar asked Oct 19 '22 08:10

Jack


1 Answers

Putting aside that Member and Credential should implement Serializable if multiple id properties without identifier type are used, your mappings are good, and this seems to be a bug in Hibernate.

Solution 1

I managed to make this work by declaring referencedColumnName in friend and requester associations in Friendship:

@Id
@ManyToOne
@JoinColumn(referencedColumnName = "username")
Member requester;

@Id
@ManyToOne
@JoinColumn(referencedColumnName = "username")
Member friend;

This way we explicitly tell Hibernate which columns the composite id references, so that it does not have to figure it out itself.

Solution 2

The solution 1 made me think of what could be the cause of the bug in Hibernate. It seems that it is somehow affected by the order in which Hibernate processes the entity mappings. If you explicitly declare the referenced column, everything works fine, otherwise it seems that Hibernate does not know all the details about the referenced column at the time it builds the composite key.

So I changed the order in which I add annotated classes to the session factory configuration to:

Credential
Member
Friendship

and then everything worked with your original mappings (after implementing Serializable in Member and Credential).

I added the classes in this order programmatically to the Configuration class, but I assume the same effect could be achieved by specifying this order in the persistence.xml or hibernate.cfg.xml:

<class>Credential</class>
<class>Member</class>
<class>Friendship</class>

Nevertheless, this solution is just for demonstrative purposes (you or someone else can later reorder the classes without keeping this issue in mind), so I suggest using solution 1.

Note

You know your use cases better, but in my personal opinion you should use @IdClass or @EmbeddedId since they are standardized in JPA; multiple id properties without identifier type is a Hibernate specific feature. Besides being able to easier construct the primary key object by which you will search and query the corresponding entities, a dedicated PK object is usually much lighter and offers better performance when serialized, especially if second level cache is enabled.

like image 51
Dragan Bozanovic Avatar answered Nov 01 '22 13:11

Dragan Bozanovic