Thank you for your answer.
I think, I better add some clarity. A=user, B=client, C=user_client tables. User entity may contain one or more Client entities, while Client entity in reality will not contain any User entities and has no appropriate field. Though, database structure does contain many-to-many table and is not available for modification.
As you said, I created a named query like this:
Code:
SELECT c FROM Client c JOIN c.User u WHERE u.id = :userId
But I get this error:
Quote:
Unable to build EntityManagerFactory
I presume, this is because my Client entity does not contain any User fields or properties, so Hibernate cannot process "c.User" part of HQL.
In regard to your last piece of xml code, I'm not sure where it goes, since I have no hibernate configuration files which would define entities relationship.
Here are my two entities:
Code:
@Entity
@NamedQueries ({
@NamedQuery(name="Client.findByEntityId", query="SELECT c FROM Client c WHERE c.entityId = :entityId"),
@NamedQuery(name="Client.findByUserId", query="SELECT c FROM Client c JOIN c.User u WHERE u.id = :userId")}
)
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private Long entityId;
...
getters and setters skipped, no annotations below this line
Code:
@Entity
@Table(name="User")
@NamedQueries({
@NamedQuery(name = "User.login", query = "SELECT t FROM User t WHERE t.name = :username and t.password=:password")
})
public class User implements Serializable, XMLSerializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToMany
protected List<Client> clients = new ArrayList<Client>();
private String name;
private String password;
private Long role;
private String realName;
...
getters and setters skipped, no annotations below this line
As you can see, there is OneToMany annotation, though as I said, database dictates many-to-many. I guess, the problem is not the query itself, but something is wrong with my entities description.