休眠ManyToMany [英] Hibernate ManyToMany

查看:110
本文介绍了休眠ManyToMany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:
我有两个表Secret_Agent和Secret_Mission。两者都有 @ManyToMany 彼此之间的关系,因为许多秘密特工可以执行相同的秘密任务,并且同一秘密特工可以被授予许多秘密任务。

 表SECRET_AGENT 

柱SecretAgentId,SecrentAgentName

表SECRET_MISSION

柱SecretMissionId,SecrentMissionName

JOIN表

SECRET_AGENT_MISSION

柱:SecretAgentId,SecretMissionId



Java代码:

  class Secret_Agent {




@ManyToMany(级联= CascadeType.ALL)
@JoinTable(名称= SECRET_AGENT_MISSION,joinColumns = {@JoinColumn(名称= SecretAgentId)},inverseJoinColumns = {@JoinColumn(任务;



}

类Secret_Mission {


$秘密任务{




@ManyToMany(mappedBy =missions)
private List< Secret_Agent>座席;



}

问题:
I我试图通过下面提到的代码获取没有通过Hibernate Criteria分配的秘密任务的所有秘密特工:

 条件标准= session.createCriteria(Secret_Agent.class); 

criteria.add(Restrictions.isNull(missions));

然而,它不起作用,我在尝试使用Projections和QBE的运气。 另外一种方法是使用Crieteria协会,但Associa只有 @OneToMany @ManyToOne



请在这里帮我。

解决方案

映射是正确的:使用 Restrictions.isEmpty(missions) - 将集合值属性约束为空(来自javadoc)

PS:在Hibernate中,init映射中的init集合是最佳实践:

  @ManyToMany(cascade = CascadeType.ALL )
@JoinTable(名称= SECRET_AGENT_MISSION,joinColumns = {@JoinColumn(名称= SecretAgentId)},inverseJoinColumns = {@JoinColumn(名称= SecretMissionId)}
私人列表与LT; Secret_Mission> ; tasks = new ArrayList< Secret_Mission>();

并且对 Secret_Agent.agents


Context: I have two tables Secret_Agent and Secret_Mission. Both have a @ManyToMany relationship with each other since many secret agents can be given to perform the same secret mission and the same secret agent can be given many secret missions.

Table SECRET_AGENT

Columns SecretAgentId, SecrentAgentName

Table SECRET_MISSION

Columns SecretMissionId, SecrentMissionName

JOIN Table

SECRET_AGENT_MISSION

Columns: SecretAgentId,SecretMissionId

Java Code:

class Secret_Agent {
.
.
.

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "SECRET_AGENT_MISSION", joinColumns = { @JoinColumn(name = "SecretAgentId") }, inverseJoinColumns = { @JoinColumn(name = "SecretMissionId") }
private List <Secret_Mission> missions;
.
.
.
}

class Secret_Mission {
.
.
.

@ManyToMany(mappedBy = "missions")
private List <Secret_Agent> agents;
.
.
.
}

Problem: I am trying to fetch all secret agents who do not have a secret mission assigned through Hibernate Criteria in the below mentioned code:

Criteria criteria = session.createCriteria(Secret_Agent.class);

criteria.add(Restrictions.isNull("missions"));

However, it doesn't work. I am trying my luck with Projections and QBE meanwhile.

Another way is to use the Crieteria Association, but Association would work only with @OneToMany and @ManyToOne!

Please help me here.

解决方案

I'm assuming your mapping is correct: use Restrictions.isEmpty("missions") - Constrain a collection valued property to be empty (from javadoc)

PS: In Hibernate is best pratice to init collection in mapping as:

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "SECRET_AGENT_MISSION", joinColumns = { @JoinColumn(name = "SecretAgentId") }, inverseJoinColumns = { @JoinColumn(name = "SecretMissionId") }
private List <Secret_Mission> missions = new ArrayList<Secret_Mission>();

and do the same for Secret_Agent.agents

这篇关于休眠ManyToMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆