Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA OneToMany and ManyToOne Relationships
    primarykey
    data
    text
    <p>I have three classes one of the name is User and this user have other classes instances. Like this;</p> <pre><code>public class User{ @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL) public List&lt;APost&gt; aPosts; @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL) public List&lt;BPost&gt; bPosts; } public class BPost extends Post { @ManyToOne(fetch=FetchType.LAZY) public User user; } public class APost extends Post { @ManyToOne(fetch=FetchType.LAZY) public User user; } </code></pre> <p>it's working like this but generates emty tables in db. Which have to contains foreign keys. When I tried to use mappedBy and JoinColumn annotains I got failed. How can I resolve this?</p> <p><strong>Extra information:</strong></p> <p>When I changed with;</p> <pre><code> @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="id") public User user; </code></pre> <p>and</p> <pre><code> @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="id") public List&lt;APost&gt; aPosts; </code></pre> <p>I'm getting</p> <p>A JPA error occurred (Unable to build EntityManagerFactory): Repeated column in mapping for entity: models.post.APost column: id (should be mapped with insert="false" update="false")</p> <p><strong>Final Edit:</strong> Finally, I was totaly wrong about jpa annotaions. :( When i change</p> <pre><code>@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="id") </code></pre> <p>to</p> <pre><code>@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="user") </code></pre> <p>and</p> <pre><code>@ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="user_id") </code></pre> <p>everthing works ok. :)</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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