Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that JPA, by default, will not allow proper annotations for bi-directional, shared keys, one-to-one relationships. Hibernate has the perfect construct just for that, but please mind the order properly, as well as the specific parameters to the annotations (you can also read the hibernate reference, but their example will not work unless you preset the dependent object first, which can be avoided with the method bellow - which basically saves both objects automatically, in one go). In this example, each job is processing a message. Job is the "parent" and message is the dependent object. The primary key of message will be the job id (for eliminating one extra column in the message table. You can also create an id in the message table, if you need to use message outside the job scope, using the same setup as bellow). When a job is created and a message attached, hibernate will automatically save both objects when jobDao.save is called.</p> <pre><code>@Entity @Table public class Job implements Serializable{ private Long id; @Id @GeneratedValue public Long getId() { return id; } public void setId(Long id) { this.id = id; } @OneToOne(cascade = CascadeType.ALL, mappedBy = "job") public Message getMessage() { return message; } public void setMessage(Message message) { this.message = message; message.setJob(this); } } </code></pre> <p>And now the dependent message:</p> <pre><code>@Entity @Table public abstract class Message implements Serializable { private Job job; @Id @GeneratedValue(generator = "foreign") @GenericGenerator( name = "foreign", strategy = "foreign", parameters = {@org.hibernate.annotations.Parameter(name = "property", value = "job")}) @OneToOne(optional = false, cascade = CascadeType.ALL) public Job getJob() { return job; } public void setJob(Job job) { this.job = job; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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