java - Eclipselink:从 JOIN 查询创建对象

标签 java sql eclipse eclipselink

我有一个 SQL 查询

SELECT * FROM Thing AS a JOIN Thing_Property AS b ON a.id=b.Thing_ID 
   JOIN Property AS c ON b.properties_ID = c.id 
   JOIN Item AS d ON c.item_ID = d.id 
   ORDER BY a.name, d.name 

我用 Eclipselink 创建我的对象模型。 这是模型:

@SuppressWarnings("serial")
@Entity
public class Thing implements Serializable {
     @Id
     @GeneratedValue(strategy = GenerationType.TABLE)
     private int id;
     private String name;
     @OneToMany(cascade=CascadeType.ALL)
     @PrivateOwned
     private List<Property> properties = new ArrayList<Property>();
     ...
     // getter and setter following here
}
public class Property implements Serializable { 
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private int id;

    @OneToOne
    private Item item;

     private String value;
     ...
     // getter and setter following here
}
public class Item implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private int id;
    private String name;
     ....
     // getter and setter following here    
}
// Code end

但我不知道如何让 Eclipselink 从该查询创建模型。你能帮忙吗?

最佳答案

您将需要使用 API EntityManager.createNativeQuery(String sqlString, Class resultClass);

entityManager.createNativeQuery("SELECT * FROM Thing AS a JOIN Thing_Property AS b ON a.id=b.Thing_ID JOIN Property AS c ON b.properties_ID = c.id JOIN Item AS d ON c.item_ID = d.id ORDER BY a.name, d.name ", Thing.class);

关于java - Eclipselink:从 JOIN 查询创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2578127/

相关文章:

Java,如何从字符串实例化 HttpCookie,有什么方便的方法吗?

python - SQLAlchemy 获取最大值

java - 开发 eclipse 插件来解析 java 程序的程序

java - 在哪里设置空指针异常

c++ - 调试 Opencv Android 应用程序

java - 如何扫描打开的窗口

Java 使用正则表达式来匹配测验的模式

Java - 如何将 throwable 转换为新的 throwable 并将其抛出?

sql - 唯一索引的分区列必须是索引键错误的子集

php - SQL/PHP 如何验证日期是否在日期范围之间?