Java jpa(EclipseLink)生成的左连接查询错误
我有trhee实体(这是我的简化版本),数据库中定义的Java jpa(EclipseLink)生成的左连接查询错误,java,jpa,eclipselink,Java,Jpa,Eclipselink,我有trhee实体(这是我的简化版本),数据库中定义的Entity1和Entity2之间没有关系。我需要选择(使用外部联接)这样的记录,其中Entity1和Entity2中的代码以及国家/地区是相同的: 非常简单的JPA查询: 选择a、b 来自Entity1A 左连接实体2b 关于a.code=b.code a.country=b.country 但当我这样做时,生成了错误的查询: 选择t0.ID、t0.code、t0.COUNTRY\u ID、t1.ID、t1.code、t1.COUNTRY
Entity1
和Entity2
之间没有关系。我需要选择(使用外部联接)这样的记录,其中Entity1
和Entity2
中的代码以及国家/地区是相同的:
非常简单的JPA查询:
选择a、b
来自Entity1A
左连接实体2b
关于a.code=b.code
a.country=b.country
但当我这样做时,生成了错误的查询:
选择t0.ID、t0.code、t0.COUNTRY\u ID、t1.ID、t1.code、t1.COUNTRY\u ID
从表1到表0
左外连接凸舌2 t1打开((t0.CODE=t1.CODE)和(t0.COUNTRY_ID=t2.ID))
,c t2
式中(t2.ID=t1.COUNTRY\u ID)
我的意见是,正确的查询包含
左外连接t2 t1打开((t0.code=t1.code)和(t0.COUNTRY\u ID=t1.COUNTRY\u ID))
不加入t2
细节
好的,再现问题的所有细节如下:
我有一个maven项目,pom.xml为:
4.0.0
贝特里斯塔
package entity; // in "src/main/java"
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table( name = "c" )
public class Country {
@Id
long id;
@Column
private String code;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
package entity; // in "src/main/java"
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
@Entity
@Table( name = "tab1" )
public class Entity1 {
@Id
private long id;
@Column
private String code;
@JoinColumn(name = "COUNTRY_ID")
private Country country;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
}
package entity; // in "src/main/java"
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
@Entity
@Table( name = "tab2" )
public class Entity2 {
@Id
private long id;
@Column
private String code;
@JoinColumn(name = "COUNTRY_ID")
private Country country;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
}
package tests; // in "src/test/java"
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:commons-test.xml" })
public class LeftJoinTest extends AbstractTransactionalJUnit4SpringContextTests {
@PersistenceContext
protected EntityManager em;
@Test
public void test() {
Assert.assertNotNull(em);
}
@Test
public void testLeftJoin() {
StringBuilder queryString = new StringBuilder();
queryString.append(" select a, b " );
queryString.append(" from Entity1 a ");
queryString.append(" left join Entity2 b " );
queryString.append(" on a.code = b.code ");
queryString.append(" and a.country = b.country ");
Query query = em.createQuery(queryString.toString());
List list = query.getResultList();
Assert.assertEquals( 1, list.size() );
Object[] oa = (Object[])list.get(0);
Assert.assertEquals( 2, oa.length );
System.out.println(list);
}
}