简体   繁体   English

带有SQL查询的休眠注释

[英]Hibernate Annotations with SQL query

I want to create a hibernate SQL query and cast it to an object without creating the table. 我想创建一个休眠的SQL查询并将其强制转换为一个对象,而不创建表。

for example i have 例如我有

StringBuilder query = new StringBuilder();
query.append("SELECT groupId, categoryId, name FROM AssetCategory");

SQLQuery sqlQuery = session.createSQLQuery(query.toString());
sqlQuery.addEntity("Categories", Categories.class);

List<Categories> list = sqlQuery.list();

The category object is declared like this : 类别对象的声明如下:

@Entity
public class Categories implements Serializable {

@Column(name = "name")
String name;

@Column(name = "categoryId")
Long categoryId;

@Column(name = "groupId")
Long groupId;

Of course this cant work because there is no ID. 当然,由于没有ID,因此无法工作。 And this table does not need to be created either. 并且该表也不需要创建。 So how would i go about declaring this ? 那么我该如何宣布呢?

Any hints ? 有什么提示吗?

I just cant find the documentation. 我只是找不到文档。

you can use a SQLTransformer to do this.. you wont need the annotations in the pojo even. 您可以使用SQLTransformer来执行此操作。您甚至不需要pojo中的注释。

read this. 读这个。 it will help you realize what you should be doing. 它将帮助您意识到应该做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM