My problem statement is :- I want to call a stored procedure in hibernate and i want to map each column to class attributes after performing certain operations on the column returned by the stored procedure.. As any hibernate query return a list of object instead of resultset.. so how can i do it in hibernate...
I know in spring we can do it using jdbcTemplate Map row concept easily but I want to use Hibernate only..
More details can be found out in my prev question:-
Alternative to NamedParameterJDBC template row mapper in Hibernate
There are times we have a class, we would like to fill with data according the data returned from a query. The class is a simple POJO and not an Hibernate entity, so Hibernate won’t recognize this class. This can be done in Hibernate by using Transformers.
(UserActivityStat)hibernateSession.createQuery("select count(*) as totalPhotos from Photo p where p.user = :user").setResultTransformer(Transformers.aliasToBean(UserActivityStat.class)).uniqueResult();
In the above example totalPhotos is a property of Class UserActivityStat which is not a HibernateEntity. Using transformers you can achieve your result.
Related
How to retrieve/display column names and its data based on dynamic query in spring data jpa?
Example dynamic query be like
Select empid, empname, address from emp (or)
Select productiD, pname, price, pquantity from Product
I don't want to map entity just need to display data?
For a completely dynamic query Spring Data JPA doesn't offer any special support, so in order to integrate it into your repository you'll need to define a custom method.
Since you only want a Map anyway the easiest way might be to use a SQL query, execute it with a JdbcTemplate and have a simple ResultSetExtractor that inserts the values from the ResultSet into a Map.
If you want to utilise the mapping information of JPA you might construct a query using the Criteria API where you explicitly specify all the columns you want and specify Tuple as target type.
Using Spring Boot and Spring Data JPA, I have a table which represents an enum value, and a corresponding entity that I want that one of its field will be 'all the possible values the enum can have', i.e. a field possibleValues that will be a select all on the other table. Preferably I don't want to have a relationship like #ManyToMany since:
It will always be a select all, I don't want to save to the database all the options and update them each time the enum values table changes.
I am going to have several enums, so for each enum create another many-to-many is less than ideal.
I've tried to find something like #Formula that will let me select all the values from another table, but it doesn't seem to work:
#Transient
#Formula("select e.name from EnumTable e")
private List<String> possibleValues;
results in possibleValues always being null, and if I remove the #Transient I have to define the relationship between the two entities.
For Enum value in database, I recommend using string (for object, you can use JSON to convert first before store and covert back after retrieve )
I don't want to show all my attributes on a table, so I'd like to select only a few columns and then map their values to a bean, something similar to Hibernate's Transformers.aliasToBean(T.class), but using JPA and Criteria.
I'm able to get a List<Tuple>, how could I transform it into a List<T>?
As the the title says, is it possible?
Within JPQL/Criteria queries you can provide arguments to an objects constructor within the query so that the query returns those objects for you. For example:
"SELECT new package.BeanClass(e.attribute1, e.attribute2) from Entity e"
With native SQL queries, if you are using JPA 2.1, you can use ConstructorResult to build the same instances:
http://en.wikibooks.org/wiki/Java_Persistence/Querying#ConstructorResult_.28JPA_2.1.29
But if you are not able to use Jpa 2.1, you will need a provider specific solution such as http://onpersistence.blogspot.com/2010/07/eclipselink-jpa-native-constructor.html
Example:
List<Object[]> list = em.createQuery(
"SELECT 'Foo', 123 FROM IrcEvent ev", Object[].class).getResultList();
What I don't like in that example:
How do I know the table name? Can't I specify the entity class instead?
How do I know the column name? jOOQ provides auto-completion by creating a DSL from the database schema.
There could be syntax errors everywhere.
What I basically want is something like
entityManager.deleteAll(EntityClass.class);
to delete the rows of an antire table (for example).
JPA 2 Criteria API http://docs.oracle.com/javaee/6/tutorial/doc/gjitv.html is for creating type safe queries programmatically, but it does not support deletes
I want to execute the following sql in hibernate:
SELECT emp.*, utilsPkg.getEmployeeDisplayName(emp.id) FROM employee emp;
So far so good...
The thing is - I need it to be fetched to an entity - so I can update the employee.
Of course that the pl\sql function is not updateable nor part of the actual table...
How can I generate such an entity in hibernate - with a field that is calculated and not updateable?
Many thanks!
Using the #Formula annotation, as explained in the Hibernate documentation:
Sometimes, you want the Database to do some computation for you rather
than in the JVM, you might also create some kind of virtual column.
You can use a SQL fragment (aka formula) instead of mapping a property
into a column. This kind of property is read only (its value is
calculated by your formula fragment).
Use the #Transient annotation on the getter method for that property.
I'm not sure if I fully understand your question, but you can always use Hibernate entity lifecycle callbacks or you can provide it directly in your query:
select new MyEntity(o.column1, o.column2, utilsPkg.getEmployeeDisplayName(o.id)) from MyEntity as o where o.id = 5
Of course you must implement appropriate constructor.