Retrieving a non managed entity from database using hibrnate.
If an entity is not mapped to the configuration file and we try to retrieve it from the database generatlly it throws mapping exception.
To get ride of such a situation we can user ResultTranform to retrieve the datafrom the database and map it to a bean which is not an entity or mapped in configuration.
Cautions:Attributes name and datatype in bean and the database should be same.
See the below the snap of code to implement ResultTransformer.
Session session=Connector.getSession();
Transaction tx=session.beginTransaction();
Query query=session.createSQLQuery("SELECT * FROM customer");
List<Customer> list= query.setResultTransformer(Transformers.aliasToBean(Customer.class)).list();
Note: Customer is a non mapped entity it is simple a java bean.
Subscribe to:
Post Comments (Atom)
Spring Boot Config Server and Config Client.
In Spring cloud config we can externalise our configuration files to some repository like GIT HUT, Amazon S3 etc. Benefit of externalising ...
-
As we know that TreeSet by default store the unique value it doesn’t check case of the value that is being added. For example if...
-
We can extract text from pdf file using itext 2.1.6 PdfReader readerN = new PdfReader("pdfFilename"); OutputStream...
-
We mostly use hibernate ‘s sessionFactory.openSession() method which keeps track of all events of hibernate along with managing first ...
Great!!
ReplyDeleteBut, how can we map customer table columns with Customer.class properties?
Your answer will be appreciated.
Thanks,
Sanjay
Hi Sanjay,
DeleteIf the customer bean has the same columns name with setter and getter as defined in the database table then resultTransformer will automatically map the column to the customer bean.