在Customer.java实体类中,我有以下代码:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer", fetch = FetchType.EAGER)
@MapKey(name = "key")
@JoinFetch(JoinFetchType.OUTER)
private Map<String, CustomerSuppInfo> suppInfoMap....
发现当我查询客户实体时,eclipselink生成的SQL没有连接获取CustomerSuppInfo映射(它生成1个SQL来选择customer表,然后另外1个SQL来按id选择supp_info表) 我想要的只是生成1个SQL并选择整个客户相关字段,以便最小化SQL生成的/ EJB调用#。
这两个注释是不是可以共存? 我试图找到JPA文档,但似乎没有运气:(
答案 0 :(得分:1)
如果注释“@JoinFetch(JoinFetchType.OUTER)”未导致EclipseLink连接为拥有实体上的所有查询提取“suppInfoMap”,并且您没有使用XML覆盖此映射,则这是一个错误。您应该提交EclipseLink错误。或者,您可以使用“JOIN FETCH”JPQL关键字直接创建查询以加入“suppInfoMap”。
答案 1 :(得分:0)
您的查询示例
import java.util.HashMap; import java.util.Map;
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.MapKey; import javax.persistence.OneToMany;
@Entity 公共类部门{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; 私有字符串名称; @OneToMany(的mappedBy = “部门”) @MapKey(名称= “名称”) 私人地图学生;
public Department() {
students = new HashMap<String, Student>();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String deptName) {
this.name = deptName;
}
public void addStudent(Student student) {
if (!getStudents().containsKey(student.getName())) {
getStudents().put(student.getName(), student);
if (student.getDepartment() != null) {
student.getDepartment().getStudents().remove(student.getName());
}
student.setDepartment(this);
}
}
public Map<String,Student> getStudents() {
return students;
}
public String toString() {
return "Department id: " + getId() +
", name: " + getName();
}
}
档案:Student.java
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne;
@Entity 公立班学生{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; 私有字符串名称;
@ManyToOne
private Department department;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public String toString() {
return "Student id: " + getId() + " name: " + getName() +
" with " + getDepartment();
}
}
文件:Helper.java
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement;
public class Helper { public static void checkData()throws Exception { 的Class.forName( “org.hsqldb.jdbcDriver”); Connection conn = DriverManager.getConnection(“jdbc:hsqldb:data / tutorial”,“sa”,“”); Statement st = conn.createStatement();
ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" });
while (mrs.next()) {
String tableName = mrs.getString(3);
System.out.println("\n\n\n\nTable Name: "+ tableName);
ResultSet rs = st.executeQuery("select * from " + tableName);
ResultSetMetaData metadata = rs.getMetaData();
while (rs.next()) {
System.out.println(" Row:");
for (int i = 0; i < metadata.getColumnCount(); i++) {
System.out.println(" Column Name: "+ metadata.getColumnLabel(i + 1)+ ", ");
System.out.println(" Column Type: "+ metadata.getColumnTypeName(i + 1)+ ": ");
Object value = rs.getObject(i + 1);
System.out.println(" Column Value: "+value+"\n");
}
}
}
} }
文件:Main.java
import java.util.List;
import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import javax.persistence.Query;
public class Main { static EntityManagerFactory emf = Persistence.createEntityManagerFactory(“JPAService”); static EntityManager em = emf.createEntityManager();
public static void main(String [] a)抛出异常{ em.getTransaction()开头();
Student student = new Student();
student.setName("Joe");
em.persist(student);
Department dept = new Department();
dept.addStudent(student);
em.persist(dept);
em.flush();
Query query = em.createQuery("SELECT e FROM Student e");
List<Student> list = (List<Student>) query.getResultList();
System.out.println(list);
query = em.createQuery("SELECT d FROM Department d");
List<Department> dList = (List<Department>) query.getResultList();
System.out.println(dList);
em.getTransaction().commit();
em.close();
emf.close();
Helper.checkData();
} }
文件:persistence.xml
http://java.sun.com/xml/ns/persistence/persistence“version =”1.0“&gt;