Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA entity without underlying table

Tags:

I want to create a class that can be mapped to a result extracted from the database using JPA native query. Is there a way to map an entity without an underlying table to the result? I referred to this link which allows it for hibernate. Can this be done using JPA instead?

This is my class for which I want the result to be mapped.

import java.math.BigDecimal; import javax.persistence.Entity; @Entity public class OpUsage {       String username;         BigDecimal number_of_clicks;         String accordion;     public String getUsername() {         return username;     }     public void setUsername(String username) {         this.username = username;     }     public BigDecimal getNumber_of_clicks() {         return number_of_clicks;     }     public void setNumber_of_clicks(BigDecimal number_of_clicks) {         this.number_of_clicks = number_of_clicks;     }      public String getAccordion() {         return accordion;     }      public void setAccordion(String accordion) {         this.accordion = accordion;     } } 
like image 346
Nikhil Avatar asked Mar 09 '15 15:03

Nikhil


People also ask

Is @column mandatory in JPA?

Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.

Can we create JPA entity without ID?

If your object does not have an id, but its table does, this is fine. Make the object an Embeddable object, embeddable objects do not have ids. You will need a Entity that contains this Embeddable to persist and query it. Save this answer.

Does Spring JPA work without ORM?

you can't use JPA on its own. JPA is a specification, which defines an API for object-relational mappings and for managing persistent objects and you need a JPA provider to implement it, like Hibernate, EclipseLink. Show activity on this post. No, you cannot perform CRUD operations with JPA alone.

Is @entity annotation mandatory in Hibernate?

The entity class must be annotated with the Entity annotation or denoted in the XML descriptor as an entity. So, if you use annotations for mappings, @Entity is mandated by the specification and Hibernate has to adhere to it.


1 Answers

JPA 2.1 specification defines the means to return the result from a native query to a non entity class

You should checkout the heading 3.10.16.2 Returning Unmanaged Instances especially the

3.10.16.2.2 Constructor Results

The mapping to constructors is specified using the ConstructorResult annotation element of the SqlResultSetMapping annotation. The targetClass element of the ConstructorResult annotation specifies the class whose constructor corresponds to the specified columns. All columns corresponding to arguments of the intended constructor must be specified using the columns element of the ConstructorResult annotation in the same order as that of the argument list of the constructor. Any entities returned as constructor results will be in either the new or the detached state, depending on whether a primary key is retrieved for the constructed object.

example

Query q = em.createNativeQuery(         "SELECT c.id, c.name, COUNT(o) as orderCount, AVG(o.price) AS         avgOrder" +         "FROM Customer c, Orders o " +                 "WHERE o.cid = c.id " +                 "GROUP BY c.id, c.name",         "CustomerDetailsResult");  @SqlResultSetMapping(name = "CustomerDetailsResult",         classes = {                 @ConstructorResult(targetClass = com.acme.CustomerDetails.class,                         columns = {                                 @ColumnResult(name = "id"),                                 @ColumnResult(name = "name"),                                 @ColumnResult(name = "orderCount"),                                 @ColumnResult(name = "avgOrder", type = Double.class)})         }) 
like image 173
Master Slave Avatar answered Oct 02 '22 02:10

Master Slave