Load Entity from View in JPA/Hibernate
Solution 1
You may use axtavt solution. You may also just execute your query, and transform the List<Object[]>
it will return into a List<MyClass>
explicitely. Or you may map your view as a read-only entity, which is probably the best solution, because it would allow for associations with other tables, querying through JPQL, Criteria, etc.
In my opinion, hibernate.hbm2ddl.auto
should only be used for quick n' dirty prototypes. Use the hibernate tools to generate the SQL file allowing to create the schema, and modify it to remove the creation of the view. Anyway, if it's set to update, shouldn't it skip the table creation since it already exists (as a view)?
Solution 2
Placed on your class
@Entity
@Immutable
@Subselect(QUERY)
public MyClass {....... }
Hibernate execute the query to retrieve data, but not create the table or view. The downside of this is that it only serves to make readings.
Solution 3
You can use AliasToBeanResultTransformer
. Since it's a Hibernate-specific feature, you need to access the underlying Hibernate Session
:
return em.unwrap(Session.class)
.createSQLQuery("...")
.setResultTransformer(new AliasToBeanResultTransformer(MyClass.class))
.list();
Javi
Updated on June 04, 2022Comments
-
Javi about 2 years
I have an application which uses Spring and Hibernate. In my database there are some views that I need to load in some entities. So I'm trying to execute a native query and load the class withthe data retrieved from the view:
//In my DAO class (@Repository) public List<MyClass> findMyEntities(){ Query query = em.createNativeQuery("SELECT * FROM V_myView", MyClass.class); return query.getResultList(); }
and MyClass has the same fields as the column names of the view.
The problem is that Hibernate can't recognize MyClass because it's not an entity (it's not annotated with @Entity)
org.hibernate.MappingException: Unknown entity
If I put MyClass as an entity the system will put try to create/update a table for that entity, because I have configured it :
<property name="hibernate.hbm2ddl.auto" value="update"/>
So I come into these questions:
- Can I disable "hibernate.hbm2ddl.auto" just for a single entity?
- Is there any way to load the data from a view into a non-entity class?
- If not, what would be the best way in my case for loading the data from a view into a class in hibernate?
Thanks
-
DataNucleus over 12 yearsFWIW, Other JPA implementations support a result class which is not an entity, and JPA 2.0 spec says nothing about only Entity types.
-
Javi over 12 yearsTrying it I'm getting this error: "ERROR org.hibernate.property.BasicPropertyAccessor expected type: java.lang.Long, actual value: java.math.BigInteger". Do I have to set all numbers to BigInteger or is there any way it can cast them to Long? (those numbers are from another entity which is a long value).
-
axtavt over 12 years@Javi: You can declare fields of your class as
BigInteger
, or apply explicit casts to columns in your SQL query (instead of*
).