[英]Hibernate Left Outer Join
I have a Hibernate service method as such: "SELECT sc FROM SecurityContact sc WHERE sc.securityId=:securityId2"
. 我有一个Hibernate服务方法:
"SELECT sc FROM SecurityContact sc WHERE sc.securityId=:securityId2"
。 securityId2 is passed in by the user. securityId2由用户传入。 Each SecurityContact has a many to one relationship with a Contact, so Hibernate automatically calls a join when this query runs.
每个SecurityContact与Contact都有多对一的关系,因此Hibernate会在运行此查询时自动调用连接。 However, the join that Hibernate always runs is an inner join, which will not work properly for my purposes.
但是,Hibernate总是运行的连接是一个内部连接,它不能用于我的目的。 Is there any way to force Hibernate to internally generate a left outer join instead?
有没有办法强制Hibernate在内部生成左外连接? Here is the code for the SecurityContact class:
以下是SecurityContact类的代码:
/**
* The persistent class for the SecurityContact database table.
*
*/
@Entity
@FXClass(kind=FXClassKind.REMOTE)
public class SecurityContact implements Serializable {
private static final long serialVersionUID = 1L;
@Transient private String uid;
@FXIgnore
public String getUid() {
if (uid == null) {
uid = "" + securityContactId;
}
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="securityContact_id")
private Long securityContactId;
@Column(name="security_id")
private String securityId;
@Column(name="create_date")
private String createDate;
@Column(name="create_user")
private String createUser;
@Column(name="modify_date")
private String modifyDate;
@Column(name="modify_user")
private String modifyUser;
//bi-directional many-to-one association to AgentContact
@ManyToOne
@JoinColumn(name="agent_id", referencedColumnName="contact_id")
private AgentContact agentContact;
//bi-directional many-to-one association to AuditContact
@ManyToOne
@JoinColumn(name="audit_id", referencedColumnName="contact_id")
private AgentContact auditContact;
public SecurityContact() {
}
@FXKeyColumn
public Long getSecurityContactId() {
return this.securityContactId;
}
public void setSecurityContactId(Long securityContactId) {
this.securityContactId = securityContactId;
}
public String getSecurityId() {
return this.securityId;
}
public void setSecurityId(String securityId) {
this.securityId = securityId;
}
public String getCreateDate() {
return this.createDate;
}
public void setCreateDate(String createDate) {
this.createDate = createDate;
}
public String getCreateUser() {
return this.createUser;
}
public void setCreateUser(String createUser) {
this.createUser = createUser;
}
public String getModifyDate() {
return this.modifyDate;
}
public void setModifyDate(String modifyDate) {
this.modifyDate = modifyDate;
}
public String getModifyUser() {
return this.modifyUser;
}
public void setModifyUser(String modifyUser) {
this.modifyUser = modifyUser;
}
@FXManyToOne(parent="parent", property="contactId")
public AgentContact getAgentContact() {
return this.agentContact;
}
public void setAgentContact(AgentContact agentContact) {
this.agentContact = agentContact;
}
@FXManyToOne(parent="parent", property="contactId")
public AgentContact getAuditContact() {
return this.auditContact;
}
public void setAuditContact(AgentContact auditContact) {
this.auditContact = auditContact;
}
}
Try fetch=select
on the many-to-one matching 尝试
fetch=select
多对一匹配
Also see http://community.jboss.org/wiki/AShortPrimerOnFetchingStrategies and http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html 另见http://community.jboss.org/wiki/AShortPrimerOnFetchingStrategies和http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html
Relationship: Account many -- Employee one 关系:账户多 - 员工一
the configuration in xml: xml中的配置:
<many-to-one name="employee" class="active.security.domain.Employee" lazy="false">
<column name="EmpId" />
</many-to-one>
java code : java代码:
Session session = HibernateUtil.getCurrentSession();
Criteria criteria = session.createCriteria(Account.class);
criteria.add(Restrictions.eq("application.id", applicationID));
List<Account> list = criteria.list();
the key is to use criteria.list(); 关键是使用criteria.list();
According to the Hibernate documentation the hibernate query language should support this. 根据Hibernate文档,hibernate查询语言应该支持这一点。 (At least in version 3.3)
(至少在3.3版本中)
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-joins http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-joins
Try to create your query like this: 尝试创建这样的查询:
Query query = entityManager.createQuery("Select sc from SecurityContact as sc " +
"left outer join sc.agentContact as c " +
"where sc.securityId=:securityId2";
EDIT: Changed the contact property of sc to the agentContact property you supplied in your question details. 编辑:将sc的联系人属性更改为您在问题详细信息中提供的agentContact属性。
I had the following configuration: 我有以下配置:
<many-to-one column="user_id" lazy="proxy" fetch="join" insert="false" name="user" not-null="true" update="false" not-found="ignore" />
The user_id column was not-null, but I wanted a LEFT OUTER JOIN
(instead of the INNER JOIN
I was getting), because it was just a DB hack - sometimes user_id = 0
which wasn't mapped to any row in the user table (0 as a replacement to NULL). user_id列不是null,但是我想要一个
LEFT OUTER JOIN
(而不是我得到的INNER JOIN
),因为它只是一个DB hack - 有时候user_id = 0
,它没有映射到用户表中的任何行(0代替NULL)。 I didn't want the fetch=select
mode 我不想要
fetch=select
模式
After a lot of debugging in the Hibernate intervals I set not-null="false"
which solved the issue for me :) (I got the LEFT OUTER JOIN
). 经过Hibernate间隔的大量调试后,我设置了
not-null="false"
,这解决了我的问题:)(我得到了LEFT OUTER JOIN
)。 Hopefully someone will find it useful (btw.: I'm using Hibernate 3.6.0.Final). 希望有人会觉得它很有用(顺便说一下:我正在使用Hibernate 3.6.0.Final)。
I had a similar problem. 我遇到了类似的问题。 I was having a SiebelUser DAO.
我有一台SiebelUser DAO。 Each SiebelUser related to a team with many to one relationship.
每个SiebelUser都与具有多对一关系的团队相关。 Some SiebelUser were having has userid = 0 as foreign key for which no primary key was present in Users table.
一些SiebelUser的userid = 0作为外键,Users表中没有主键。 So naturally the fetch from SeibelUsers table was ignoring users with userd id =0.
因此,SeibelUsers表中的提取自然会忽略用户id = 0的用户。 This was earlier configuration
这是早期的配置
<many-to-one name="teamid" class="com.hewitt.wlm.pojo.Tblteams"> <column name="TEAMID" length="4" not-null="true" /> </many-to-one>
So, In order to solve my problem I changed the config to . 所以,为了解决我的问题,我将配置更改为。 But it didn't work for me.
但它对我不起作用。
<many-to-one name="teamid" class="com.hewitt.wlm.pojo.Tblteams" **fetch="join" not-null="false" lazy="proxy" not-found="ignore"**> <column name="TEAMID" length="4" not-null="**false**" /> </many-to-one>
Finally I changed my query to explicitly do a left outer join like this giving the specifc path from SiebelUsers to Users table. 最后,我将查询更改为显式执行左外连接,这样就给出了从SiebelUsers到Users表的特定路径。
select property1, property2, ...
from from **SiebelUser s left outer join s.team t**
where property1='x'
This worked for me. 这对我有用。
Note that my SiebelUser class was having Team object as its property(as defined in the path above). 请注意,我的SiebelUser类将Team对象作为其属性(如上面的路径中所定义)。 Hope it helps someone.
希望它可以帮助某人。
在你的hibernate配置中,将use_outer_join属性设置为true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.