Need an example of the primary display of @OneToOne in Hibernate

Can someone please give me an example of unidirectional mapping of primary keys @OneToOne in Hibernate? I have tried many combinations, and so far the best I got is this:

@Entity @Table(name = "paper_cheque_stop_metadata") @org.hibernate.annotations.Entity(mutable = false) public class PaperChequeStopMetadata implements Serializable, SecurityEventAware { private static final long serialVersionUID = 1L; @Id @JoinColumn(name = "paper_cheque_id") @OneToOne(cascade = {}, fetch = FetchType.EAGER, optional = false, targetEntity = PaperCheque.class) private PaperCheque paperCheque; } 

Whenever Hibernate tries to automatically generate a schema for the above mapping, it tries to create the primary key as a blob, and not as a long, which is the id type of PaperCheque. Can someone please help me? If I can’t get an exact solution, I’m close to do something, but I will be grateful for any answer.

+4
source share
5 answers

Your intention is to have a 1-1 relationship between PaperChequeStopMetaData and PaperCheque? If so, you cannot define an instance of PaperCheque as @Id from PaperChequeStopMetaData, you must define a separate @Id column in PaperChequeStopMetaData.

+1
source

I saved this discussion when I implemented a couple of @OneToOne mappings, I hope it can be useful to you, but we don’t let Hibernate create a database for us.

Check out the GenericGenerator annotation.

Anyway, this code works for me:

 @Entity @Table(name = "message") public class Message implements java.io.Serializable { @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @PrimaryKeyJoinColumn(name = "id", referencedColumnName = "message_id") public MessageContent getMessageContent() { return messageContent; } } @Entity @Table(name = "message_content") @GenericGenerator(name = "MessageContent", strategy = "foreign", parameters = { @org.hibernate.annotations.Parameter ( name = "property", value = "message" ) } ) public class MessageContent implements java.io.Serializable { @Id @Column(name = "message_id", unique = true, nullable = false) // See http://forum.hibernate.org/viewtopic.php?p=2381079 @GeneratedValue(generator = "MessageContent") public Integer getMessageId() { return this.messageId; } } 
+3
source

Thank you for your answers. I continued to experiment, and here is what I got:

 @Entity @Table(name = "paper_cheque_stop_metadata") @org.hibernate.annotations.Entity(mutable = false) public class PaperChequeStopMetadata implements Serializable, SecurityEventAware { private static final long serialVersionUID = 1L; @SuppressWarnings("unused") @Id @Column(name = "paper_cheque_id") @AccessType("property") private long id; @OneToOne(cascade = {}, fetch = FetchType.EAGER, optional = false, targetEntity = PaperCheque.class) @PrimaryKeyJoinColumn(name = "paper_cheque_id") @JoinColumn(name = "paper_cheque_id", insertable = true) @NotNull private PaperCheque paperCheque; @XmlAttribute(namespace = XMLNS, name = "paper-cheque-id", required = true) public final long getId() { return this.paperCheque.getId(); } public final void setId(long id) { //this.id = id; //NOOP, this is essentially a pseudo-property } } 

This, by all means, is a disgusting hack, but it gets everything I wanted. PaperCheque property attributes as usual (not shown). I ran into such a OneToOne unidirectional mapping problem and decided to solve much more complex solutions, but this time I decided that I was going to find out, so I continued to crack it. Once again, thank you both for your answers, this is very much appreciated.

+1
source

You should stay away from Hibernate OneToOne cards, this is very dangerous. see http://opensource.atlassian.com/projects/hibernate/browse/HHH-2128

you are better off using ManyToOne mappings.

0
source

Just update this question for future views.

When this question was made, I think that there was no proper solution for this problem. But with JPA 2.0 you can use @MapsId to solve this problem.

Link with the correct explanation: https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/

0
source

All Articles