resulttransformer

Hibernate native SQL query - how to get distinct root entities with eagerly initialized one-to-many association

流过昼夜 提交于 2021-02-11 14:27:04
问题 I have two entities Dept and Emp (real case changed and minimized). There is 1:n association between them, i.e. properties Dept.empList and Emp.dept exist with respective annotations. I want to get List<Dept> whose elements are distinct and have collection empList eagerly initialized, using native SQL query. session.createSQLQuery("select {d.*}, {e.*} from dept d join emp e on d.id = e.dept_id") .addEntity("d", Dept.class) .addJoin("e", "d.empList") //.setResultTransformer(Criteria.DISTINCT

Hibernate: Can I return a non-mapped list of objects that contains a List?

牧云@^-^@ 提交于 2019-12-13 18:12:34
问题 I currently use .addScalar and ResultTransformer to return a non-mapped list of objects that do not have any List variables. I'd like to modify my object to contain a List. Following an initial example provide in Hibernate documentation, I successfully accomplished something similar to: sess.createSQLQuery("SELECT ID as id, NM_CDE as nameCode, NM_VALUE as nameValue FROM EMP") .addScalar("id", Hibernate.LONG) .addScalar("nameCode", Hibernate.STRING) .addScalar("nameValue", Hibernate.STRING)

ResultTransformer in Hibernate return null

情到浓时终转凉″ 提交于 2019-12-10 20:48:51
问题 I'm using ResultTransformer to select only particular properties from entity, just i don't need all properties from entity. But the problem i faced is when a property is "one-to-many". Here is a simple example. @Entity @Table(name = "STUDENT") public class Student { private long studentId; private String studentName; private List<Phone> studentPhoneNumbers = new ArrayList<Phone>(); @Id @GeneratedValue @Column(name = "STUDENT_ID") public long getStudentId() { return this.studentId; }

Projections in NHibernate

末鹿安然 提交于 2019-11-28 01:58:25
suppose in an entity there are attributes id, username, age, address. Now I just want id and username and I use this code for it. Projections enable the returning of something other than a list of entities from a query. var proj = Projections.ProjectionList() .Add(Projections.Property("Id"), "Id") .Add(Projections.Property("Username"), "Username"); var list2 = DetachedCriteria.For<User>() .Add(Expression.Eq("Username", "lachlan")) .GetExecutableCriteria( sessionFactory.GetCurrentSession()) .SetProjection( proj ) .List(); How will I retrieve the values. In which object will these value be taken

getting result set into DTO with native SQL Query in Hibernate

别来无恙 提交于 2019-11-27 11:13:30
I have a query like below select f.id, s.name, ss.name from first f left join second s on f.id = s.id left join second ss on f.sId = ss.id If I could use HQL, I would have used HQL constructor syntax to directly populate DTO with the result set. But, since hibernate doesn't allow left join without having an association in place I have to use the Native SQL Query. Currently I am looping through the result set in JDBC style and populating DTO objects. Is there any simpler way to achieve it? Pascal Thivent You could maybe use a result transformer. Quoting Hibernate 3.2: Transformers for HQL and

Projections in NHibernate

杀马特。学长 韩版系。学妹 提交于 2019-11-26 23:36:14
问题 suppose in an entity there are attributes id, username, age, address. Now I just want id and username and I use this code for it. Projections enable the returning of something other than a list of entities from a query. var proj = Projections.ProjectionList() .Add(Projections.Property("Id"), "Id") .Add(Projections.Property("Username"), "Username"); var list2 = DetachedCriteria.For<User>() .Add(Expression.Eq("Username", "lachlan")) .GetExecutableCriteria( sessionFactory.GetCurrentSession())

getting result set into DTO with native SQL Query in Hibernate

▼魔方 西西 提交于 2019-11-26 15:27:40
问题 I have a query like below select f.id, s.name, ss.name from first f left join second s on f.id = s.id left join second ss on f.sId = ss.id If I could use HQL, I would have used HQL constructor syntax to directly populate DTO with the result set. But, since hibernate doesn't allow left join without having an association in place I have to use the Native SQL Query. Currently I am looping through the result set in JDBC style and populating DTO objects. Is there any simpler way to achieve it? 回答1