Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate Annotations: Two relation types between two entities?
    text
    copied!<p>My scenario is a users table and a pictures table. Each user can upload multiple pictures. Each user can set one of their own pictures to be their favourite picture, but this does not affect that picture being within that user's collection of pictures.</p> <p>Mapping this to hibernate has proven to be difficult. My application has a screen which displays a list of all of a user's pictures, and also says which one is their favourite. However, when the application loads the pictures, the single picture which is the favourite one does not load. Debugging in eclipse shows various fun things for that specific object in the list, perhaps it is a proxy object. Here is the entity code showing the mappings:</p> <pre><code>@Entity class Account { @Id public String username; /* Originally a Set&lt;Picture&gt;, but wanted ordering */ @OneToMany(cascade=CascadeType.ALL, mappedBy="account") @OrderBy("uploadtime DESC") public List&lt;Picture&gt; pictures; @ManyToOne(fetch = FetchType.LAZY, optional=true) @LazyToOne(LazyToOneOption.PROXY) @JoinColumn(name="favourite_picture_id") public Picture favourite_picture; /* Sometimes it is useful to get just the id, if possible without loading the entire entity */ @OneToOne(fetch = FetchType.LAZY, optional=true) @Column(insertable=false, updatable=false) public String favourite_picture_id; } @Entity public class Picture { @Id @GeneratedValue(generator="uuid") @GenericGenerator(name="uuid", strategy="uuid2") public String id; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="username") public Account account; /* This is never used, but I thought this might be required for the mapping? */ @OneToOne(mappedBy="favourite_picture") public Account account_favourite_picture; public String mimetype; public Date uploadtime = new Date(); @Column(length=1024 * 1024 * 4) public byte[] data; } </code></pre> <p>I do not know why the favourite picture fails to load in the list of pictures. Any suggestions will be highly appreciated :D</p>
 

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