Join two tables with Play Framework and JPA

How can I join two tables using java play framework and jpa, I really have a difficult conversion of my MySQL query into a jpa query.

Here is the MySQL query that I used in my old Java code:

SELECT * FROM tbl_majors
INNER JOIN tbl_lookup_user_major
ON tbl_majors.id=tbl_lookup_user_major.majorId
WHERE tbl_lookup_user_major.userId=12

//Table 1:

@Entity
@Table(name="tbl_majors")
public class Major extends Model {
    public Major(){

    }
    @Column(name="major_name")
    private String name;
    @Column(name="major_desc")
    private String description;
}

//Table 2

@Entity
@Table(name="tbl_lookup_user_major")
public class LookupUserMajor extends Model {
    public LookupUserMajor(){

    }
    private int majorId;
    private int userId;
}
+5
source share
3 answers

I don’t know if I can get the exact point here, but in the YABE training blog this connection table is used and created automatically using the Play function:

http://www.playframework.org/documentation/1.2.4/guide6#tagging

" " ( "" "" ):

@ManyToMany(cascade=CascadeType.PERSIST)
public Set<Tag> tags;

public Post(User author, String title, String content) {
    ...
    this.tags = new TreeSet<Tag>();
    ...
    this.title = title;
    this.content = content;
    ...
}

YAML :

Post(jeffPost):
    title:          The MVC application
    postedAt:       2009-06-06
    author:         jeff
    tags:           
                    - play
                    - architecture
                    - mvc

, "post_tag" , (post_ids tags_ids ).

, :

"select distinct p from Post p join p.tags as t"

- ? Java JPA Play ^^

, , "".

+1

, "xxxId" , "xxxId" - , - . JPA - .

tbl_lookup_user_major . , -- ( --, ) Major User. , Major :

@ManyToMany
@JoinTable(...) // details omitted
private Set<User> users;

JPA

select m from Major m
inner join m.users user
where user.id = :userId
+2

Jpa ...

Query query = JPA.em().createQuery(" SELECT * FROM "+User.class.getName() +" AS a JOIN "+ 

Role.class.getName()+" AS b WHERE a.roleId=b.roleId ");
0

All Articles