msingh
msingh

Reputation: 549

mapping result of native query to a POJO without having a table corresponding to it created in the DB on query execution

I am using Hibernate implementation of JPA (Hibernate version: 4.2.1, JPA version: 1.8.1). I have some native queries, the result of which are mapped to my list of custom POJOS (marked with @Entity). However, when the query executes, I get an empty table created in my MySQL schema featuring the attributes in my POJO. How do I ensure that no table gets added to my existing schema, considering that it is not possible to map native query results to a POJO without marking it as an "Entity".

POJO

package abc;
@Entity
public class Myclass
{
@Id
private Integer productId;
private String productName;
public MyClass(){
}
/**getters and setters for productId and productName**/
}

CODE SNIPPET FOR EXECUTING NATIVE QUERY

List<Integer> productIdList = new ArrayList<>();
productIdList.add(1);
productIdList.add(20);
productIdList.add(34); 
List<MyClass> myList=(List<MyClass>)(entityManager.createNativeQuery("select p.productId, p.productName from products p where p.productId IN (:productIdList)",MyClass.class).setParameter("productIdList", productIdList).getResultList());

Upvotes: 0

Views: 761

Answers (1)

Ismail Sahin
Ismail Sahin

Reputation: 2710

I had the same problem, for that I tried all possible values for hibernate.hbm2ddl.auto parameters but no luck. Simply, I would expect an option like do_nothing but in the documentation there is none like that. So my solution was to add @Table("tbl_to_remove_<YourObjectName>") to each entity and remove all newly created tables that starts with tbl_to_remove in db manually and yes each time I restart my application.

From the community documentation: here the possible options are listed

hibernate.hbm2ddl.auto Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.

e.g. validate | update | create | create-drop

Upvotes: 1

Related Questions