2011-01-13 12 views

Respuesta

5

Below Snippet puede resolver su problema.

Database Query 

CREATE OR REPLACE VIEW cameron AS 
    SELECT last_name AS surname 
    FROM author 
    WHERE first_name = 'Cameron'; 

view entity 

@Entity 
@NamedNativeQuery(name = "findUniqueCameronsInOrder", query = "select * from cameron order by surname", resultClass = Cameron.class) 
public class Cameron implements java.io.Serializable { 

    private static final long serialVersionUID = 8765016103450361311L; 

    private String surname; 

    @Id 
    @Column(name = "SURNAME", nullable = false, length = 50) 
    public String getSurname() { 
     return surname; 
    } 

    public void setSurname(final String surname) { 
     this.surname = surname; 
    } 
} 

Hibernate mapping file. 

<mapping class="examples.hibernate.spring.query.domain.Cameron" /> 

finally some test !... 

@Test 
    public void findTheCameronsInTheView() throws Exception { 
     final List<Cameron> camerons = findUniqueCameronsInOrder(); 
     assertEquals(2, camerons.size()); 
     final Cameron judd = camerons.get(0); 
     final Cameron mcKenzie = camerons.get(1); 
     assertEquals("Judd", judd.getSurname()); 
     assertEquals("McKenzie", mcKenzie.getSurname()); 
    } 
+0

Tomado de [aquí] (http://timezra.blogspot.de/2009/05/mapping-hibernate-entities-to-views.html) (supongo) sin atribución – avalancha

2

Es muy similar al mapeo de la tabla de base de datos común. Crea una entidad y usa tu nombre de vista como nombre de tabla.

@Entity 
@Table(name = "rc_latest_offer_details_view") 
public class OfferLatestDetailsViewEntity { 

@Id 
@Column(name = "FK_OFFER_ID") 
private int offerId; 

@Column(name = "MAX_CHANGED_DTM") 
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime") 
private DateTime changedDateTime; 

private BigDecimal price; 
... 
} 

A continuación, consulte las entidades de la misma forma que lo hace para la tabla normal. Trabajando en Hibernate 4, Spring 4.

Cuestiones relacionadas