Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA How to create Query with ManytoOne relationship?
    primarykey
    data
    text
    <p><b>The basic question:</b> <br> If I have an entity B with ManyToOne field x linked to another entity A, how do I get all the instances of B that have A in their x field? <br></p> <p><br> More specifically: <br> Consider the following entites:</p> <pre><code>public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "USER_ID") private Key id; @OneToMany(mappedBy = "owner", cascade = CascadeType.ALL) private List&lt;Message&gt; messages; } ic class Message { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column (name="MESSAGE_ID") private Key id; @ManyToOne(fetch = FetchType.LAZY) private User owner; private int status; } </code></pre> <p>I already have this query prepared</p> <pre><code>Query query = em.createQuery("SELECT m from Message m WHERE m.owner = :us"); </code></pre> <p><i>Here is the api for a method I want to build: </i><br> <b>Input:</b> User u, Status s <br> <b>Output:</b> List of all message with owner u and status s. <br> <br> I know I am supposed to build a query using EntityMenager but what is the proper syntax? What do I put in the part (*"SELECT m FROM Message m WHERE owner = <em>_</em> AND status="+status*).</p> <p>when I tried this:</p> <pre><code>Query query = em.createQuery("SELECT m from Message m " +"WHERE m.owner.id = :ownerId"); </code></pre> <p>I got the follwing error:</p> <pre><code>javax.persistence.PersistenceException: SELECT FROM Message m WHERE m.owner.username = :ownerID: Can only reference properties of a sub-object if the sub-object is embedded. </code></pre> <p>Thanks in advance....</p>
    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.
 

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