native-sql

Hibernate Native SQL Mapping to Entity with Selection Example

做~自己de王妃 提交于 2019-12-01 11:47:53
问题 I tried to create native sql query and map to entity class with parameter but fail. /** * @return */ public List<PoolRuleMapping> getAllPRM() { Session session = null; List<PoolRuleMapping> prmList = null; try { session = HibernateSessionFactory.getSession(); Query q = session.createSQLQuery("select * from pool_rule_mapping").addEntity(PoolRuleMapping.class); prmList = q.list(); } catch (HibernateException e) { } finally { session.close(); } return prmList; } Please provide an example. Thanks

Hibernate Native SQL Query retrieving entities and collections

ぃ、小莉子 提交于 2019-11-30 00:15:09
This is my situation, I have two basic POJO's which I've given a simple hibernate mapping : Person - PersonId - Name - Books Book - Code - Description My SQL Query returns rows that look like this : PERSONID NAME CODE DESCRIPTION -------- ---------- ---- ----------- 1 BEN 1234 BOOK 1 1 BEN 5678 BOOK 2 2 JOHN 9012 BOOK 3 My hibernate query looks like this : session.createSQLQuery("select personid, name, code, description from person_books") .addEntity("person", Person.class) .addJoin("book", "person.books") .list(); This is per section : 18.1.3 of the hibernate documentation : http://docs.jboss

Hibernate Native SQL Query retrieving entities and collections

六眼飞鱼酱① 提交于 2019-11-28 20:20:02
问题 This is my situation, I have two basic POJO's which I've given a simple hibernate mapping : Person - PersonId - Name - Books Book - Code - Description My SQL Query returns rows that look like this : PERSONID NAME CODE DESCRIPTION -------- ---------- ---- ----------- 1 BEN 1234 BOOK 1 1 BEN 5678 BOOK 2 2 JOHN 9012 BOOK 3 My hibernate query looks like this : session.createSQLQuery("select personid, name, code, description from person_books") .addEntity("person", Person.class) .addJoin("book",

Difference between query, native query, named query and typed query [closed]

谁都会走 提交于 2019-11-28 16:04:13
What are the differences between a query, a native query, a named query and a typed query? Does the 'alone-standing' query even exist, or is it just an abbreviation? In my mind, a native Query is a query written in simple sql, whereas a named query relates to entities (hibernate-mapping). Can someone explain this briefly? Abhijith Nagarajan Query Query refers to JPQL/HQL query with syntax similar to SQL generally used to execute DML statements(CRUD operations). In JPA, you can create a query using entityManager.createQuery() . You can look into API for more detail. In Hibernate, you use

Native SQL query for an Hibernate entity using @Formula results in NullPointerException

旧时模样 提交于 2019-11-28 13:21:38
I've got an simple Hibernate entity for which I use the @Formula annotion: @Id private Long id; private String name; @Formula("(select count(f.*) from foo f where f.id = id)") private long bar; When I try to load an entity with a native SQL Query: EM.createNativeQuery("SELECT f.*, count(something) as bar FROM foo f WHERE f.name = '...'", Entity.class) I get an NullPointerException: java.lang.NullPointerException at org.hibernate.loader.DefaultEntityAliases.intern(DefaultEntityAliases.java:193) at org.hibernate.loader.DefaultEntityAliases.getSuffixedPropertyAliases(DefaultEntityAliases.java:151

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

Native SQL query for an Hibernate entity using @Formula results in NullPointerException

泪湿孤枕 提交于 2019-11-27 07:46:05
问题 I've got an simple Hibernate entity for which I use the @Formula annotion: @Id private Long id; private String name; @Formula("(select count(f.*) from foo f where f.id = id)") private long bar; When I try to load an entity with a native SQL Query: EM.createNativeQuery("SELECT f.*, count(something) as bar FROM foo f WHERE f.name = '...'", Entity.class) I get an NullPointerException: java.lang.NullPointerException at org.hibernate.loader.DefaultEntityAliases.intern(DefaultEntityAliases.java:193

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