简体   繁体   中英

Hibernate Annotations with SQL query

I want to create a hibernate SQL query and cast it to an object without creating the table.

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. 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.

read this. it will help you realize what you should be doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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