Home  >  Hibernate

@SqlResultSetMapping and @NamedNativeQuery in Hibernate

By Arvind Rai, January 31, 2013
In Hibernate Annotation to use native query, @SqlResultSetMapping and @NamedNativeQuery is available. @SqlResultSetMapping provides the inner annotation property as @EntityResul, @FieldResult and @ColumnResult. @NamedNativeQuery fetches the native query and @SqlResultSetMapping maps the query result with the entity.
Car.java
package com.concretepage.persistence;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.ColumnResult;
import javax.persistence.Entity;
import javax.persistence.EntityResult;
import javax.persistence.FetchType;
import javax.persistence.FieldResult;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.Table;
mport org.hibernate.annotations.NamedNativeQuery;

@Entity
@Table(name="car")
@SqlResultSetMapping(name="carkey",
entities=@EntityResult(entityClass=Car.class,
fields = {
        @FieldResult(name="id", column = "id"),
        @FieldResult(name="name", column = "name"),
        @FieldResult(name="dimension.length", column = "length"),
        @FieldResult(name="dimension.width", column = "width")
       }),
       columns = { @ColumnResult(name = "area")})

@NamedNativeQuery(name="carkey",query="select id as id,name as name,length as length,width as width,length*width as area from Car", resultSetMapping="carkey")
public class Car implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@Column(name="id")
	private int id;
	
	@Column(name="name")
	private String name;
	
	@ManyToOne(fetch= FetchType.LAZY)
	@JoinColumns( {
        @JoinColumn(name="id", referencedColumnName = "id")
    } )
	private Dimension dimension;
	
    public Car(int id,String name){
    	this.id=id;
    	this.name=name;
    }
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Dimension getDimension() {
		return dimension;
	}

	public void setDimension(Dimension dimension) {
		this.dimension = dimension;
	}
}
Dimension.java
package com.concretepage.persistence;
mport java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="dimension")
public class Dimension implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	@Column(name="id")
	private int id;
	
	@Column(name="length")
	private int length;
	
	@Column(name="width") 
	private int width;
	
	public Dimension(int id,int length, int width){
		this.id=id;
		this.length=length;
		this.width=width;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}
}
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2018 concretepage.com | Privacy Policy | Contact Us