0

私は一対一の関係を持つ2つのエンティティを持っています:

@Entity
public class Project {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;


@OneToMany(mappedBy="project",cascade=CascadeType.ALL)
private java.util.List<RmtService> services = new ArrayList<RmtService>();

}

と:

@javax.persistence.Entity
public class Service implements Serializable{

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;

@ManyToOne
    @JoinColumn(name = "project_id")
private Project project;

    // . . .
 }  

次のような SQL クエリを生成する JPA クエリを実行する方法があります。

select * from service where project_id=?

??

私が試したすべてのクエリは、常に Project テーブルで結合を生成します。

私は試した:

select s from Service s left join fetch s.project pp where pp.id = :id 

生成する

select rmtservice0_.id as id1_1_0_, project1_.id as id1_0_1_, 
rmtservice0_.project_id as project3_1_0_, rmtservice0_.serviceType 
as serviceT2_1_0_, project1_.name as name2_0_1_, project1_.surname
as surname3_0_1_ from RmtService rmtservice0_ 
left outer join Project project1_ on rmtservice0_.project_id=project1_.id 
where   project1_.id=? 

select s from RmtService s where s.project.id = :id

これにより、次の 2 つのクエリが生成されます。

select rmtservice0_.id as id1_1_, rmtservice0_.project_id as project3_1_, 
rmtservice0_.serviceType as serviceT2_1_ from RmtService rmtservice0_ where 
rmtservice0_.project_id=?

select project0_.id as id1_0_0_, project0_.name as name2_0_0_, project0_.surname
as surname3_0_0_ from Project project0_ where project0_.id=?

どうもありがとう

4

1 に答える 1

1
SELECT s FROM Service s
WHERE s.project = :project
于 2013-07-04T07:15:27.513 に答える