日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hibernate查询结果映射到实体和map的方法

發布時間:2024/1/8 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hibernate查询结果映射到实体和map的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.sql查詢封裝對象,配置映射,把查詢出來的數據封裝到實體當中:

方法一:addEntity(A.class)設定查詢出的結果映射到相應的實體中,但是這個實體必須在hibernate的映射文件中配置了相應的映射

//集合中的Object[], Object[0]即使Person,Object[1]即使MyEvent public List<Object[]> getMyEvents() {String sql = "select p.*,e.* from person_inf as p inner join event_inf as e" + " on p.person_id=e.person_id"; List list = session.createSQLQuery(sql) .addEntity("p",Person.class) .addEntity("e", MyEvent.class) .list(); return list;}

方法二:在sql語句中使用構造方法(HQL語句)

Query q = session.createQuery("select new com.hibernate.MsgInfo(m.id, m.cont, m.topic.title, m.topic.category.name) from Msg m"); List<MsgInfo> list=q.list();

2.sql查詢封裝對象,不用配置映射文件,把查詢出來的結果封裝到實體中:
方法一:setResultTransformer(Transformers.aliasToBean(PostVO.class));//封裝到實體,必須添加addScalar()為每個字段添加類型限制,不然會報錯

public List<BusinessConfig> getBusByPid(Long proId, Long busId) throws Exception {// TODO Auto-generated method stubSession session = this.getSession();StringBuffer sql = new StringBuffer();sql.append("select distinct business_id as businessId, business_name as businessName, proj_id as pid from view1 where proj_id =");sql.append(proId);if(busId != null) {sql.append(" and business_id=").append(busId);}Query query = session.createSQLQuery(sql.toString()).addScalar("businessId", Hibernate.LONG).addScalar("businessName", Hibernate.STRING).addScalar("pid", Hibernate.LONG).setResultTransformer(Transformers.aliasToBean(BusinessConfig.class));return query.list();}

方法二:query.setResultSetMapping(”“);
使用一個預定義的ResultSetMapping命名

SQLQuery query = session.createSQLQuery("...");//使用在hbm文件中配置的自定義結果集映射query.setResultSetMapping("noteAnduthor");query.list();

setResultSetMapping的解釋如下(不推薦):

First, in SQL you have to write the name of the table, not the entity name.Second, your JOIN sentence could be invalid at SQL. There are a few ways to implement a join. I'll take the direct approach (select from both table and stating the join at the where clause).Suposing that the person entity maps a table called "TABLE_PERSON", and address maps table "TABLE_ADDRESS", a valid query would look as follows:SELECT person.NAME, person.AGE, person.SEX, address.STREET, address.CITY, address.STATE, address.ZIP FROM TABLE_PERSON person, TABLE_ADDRESS address WHERE person.ID = address.PERSON_ID One more point. Checking the hibernate documentation, I have found this example:List cats = sess.createSQLQuery("select {cat.*}, {kitten.*} from cats cat, cats kitten where kitten.mother = cat.id").setResultSetMapping("catAndKitten").list(); So, maybe the problem is not at the query itself, but on the resultSet Mapping you are using, and the way to reference the fields.Let's suppose this mapping (from the Hibernate doc):<resultset name="personAddress"><return alias="person" class="eg.Person"/><return-join alias="address" property="person.mailingAddress"/> </resultset> Then, as the exaple states, your query should define the columns between curly brackets ({}), and using the alias you have defined at the mapping:personList = session.createSQLQuery("SELECT {person.NAME}, {person.AGE}, {person.SEX}, "+"{address.STREET}, {address.CITY}, {address.STATE}, {address.ZIP} "+"FROM TABLE_PERSON person, TABLE_ADDRESS address ""WHERE person.ID = address.PERSON_ID").setResultSetMapping("personAddress").list(); Please, tell me if any of this examples works.

方法三:封裝到map集合中,封裝到map集合中,如果添加別名必須添加addScalar()為每個字段添加類型限制,不然會報錯,對于oracle中某些類型hibernate沒有做對應的映射,例如oracle中的NVARCHAR2類型,仍然需要手動添加映射關系,使用addScalar()方法:

Session session = this.getSession();StringBuffer sql = new StringBuffer();sql.append("select distinct business_id as businessId, business_name businessName, proj_id pid from view1 where proj_id =");sql.append(proId);if(busId != null) {sql.append(" and business_id=").append(busId);}Query query = session.createSQLQuery(sql.toString()).addScalar("businessId", Hibernate.LONG).addScalar("businessName", Hibernate.STRING).addScalar("pid", Hibernate.LONG).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);return query.list();

總結

以上是生活随笔為你收集整理的hibernate查询结果映射到实体和map的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。