我有一个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从该查询创建模型。你能帮忙吗?
答案 0 :(得分:2)
您需要使用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);