Question

If I have a table like this...

+------------------------------------------------------+
|                         Code                         |
+------------------------------------------------------+
| id |               target_uuid              |  code  |
+----+----------------------------------------+--------+
|  1 | "01BE898A-C5A9-4F86-B0AA-4ACCDCE81B8F" |    0   |
+----+----------------------------------------+--------+
|  2 | "E139F21E-8C93-492B-9E0F-CC773FAE832D" |    2   |
+----+----------------------------------------+--------+

...and the target_uuid could refer to a Post or User uuid field, how would I map this in Hibernate?

Assume uuid is not the primary key for Post or User here. I know, I know. Bad data model, but I'm stuck with it.

I have tried this:

@Entity
@Table(name = "User")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    @Column(name = "uuid")
    private String uuid;

    @OneToOne(mappedBy = "user")
    @JoinColumn(name = "uuid")
    private Code code;

    // getters and setters
}

@Entity
@Table(name = "Post")
public class Post {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    @Column(name = "uuid")
    private String uuid;

    @OneToOne(mappedBy = "post")
    @JoinColumn(name = "uuid")
    private Code code;

    // getters and setters
}

@Entity
@Table(name = "Code")
public class Code {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    @Column(name = "code")
    private Integer code;

    @Column(name = "target_uuid")
    private String targetUuid;

    @OneToOne
    @JoinColumn(name = "target_uuid", insertable = false, updatable = false)
    private User user;

    @OneToOne
    @JoinColumn(name = "target_uuid", insertable = false, updatable = false)
    private Post post;

    // getters and setters
}

This does not throw an exception and seems to work, but my User and Post objects keep getting NULL values for their code properties.

Can anyone shed some light on this?

Was it helpful?

Solution

As explained in the comments, this kind of association is, AFAIK, impossible to map as a bidirectional association. You can map it as a OneToOne unidirectional association from Post to Code (and from User to Code) though, by simply setting the referencedColumnName of the JoinColumn annotation to "target_uuid".

OTHER TIPS

Your code implies each post is related to 1 "code". I'm not sure what the statement "target_uuid could refer to a Post or User" implies about the relationship between user and "code"? Thus, I'll focus just on "post" and "code". You also have an ambiguous statement regarding "uuid". "uuid is not the primary key f". Ok, but is it a "key"? I'll assume it is.

Let's translate: Post has 1 "code". The means the the post table contains a foreign key:

 FOREIGN KEY (uuid) REFERENCES code(target_uuid)

The relationship is stored on the "post" table, not the "code" table.

Think of books and authors. Each book has one author. The relationship is stored with the book (not the author.)

Now the JPA (in the "Post" entity, not the "Code" entity)

@JoinColumn(name = "code_id", referencedColumnName = "target_uuid")
@OneToOne(optional = false)
private Code codeId;

To summarize you need to define the relationship on the "Post" and "User" entities. Not on the "Code" entity. Perhaps I did not understand your requirements. Hope that helps:)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top