2017-01-18 65 views
0

搜索我來到這個
Can I use hibernate query language for entities not mapped to a table? 但最近我在做一個小項目,其中兩個實體沒有受到任何映射註釋映射,仍然給我的堆棧溢出後查詢結果。 所以我很困惑,因爲帖子說它不可能在Non-Mapped Entities上查詢。是否有可能使用HQL的非映射實體

客戶

package com.mayank.bitmesra.pojo; 

import java.util.Date; 

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.PrePersist; 
import javax.persistence.PreUpdate; 
import javax.persistence.Temporal; 
import javax.persistence.TemporalType; 

@Entity 
public class Customer { 


    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private Long id; 

    private String customerId; 

    private String name; 

    private String phoneNumber; 

    @Temporal(TemporalType.DATE) 
    private Date dateOfBirth; 

    @Temporal(TemporalType.DATE) 
    private Date createdDate; 

    //for rolling back of Data 
    //using date as such of now 
    @Temporal(TemporalType.DATE) 
    private Date updatedOn; 


    //for picking up only the data that is most recent 
    //like a customer can have save with same customer id 
    //but has different address as his address might have changed up in near future 
    //will try to handle this in near future 
/* @Temporal 
    private Date lastPickedUpDate;*/ 


    public String getCustomerId() { 
     return customerId; 
    } 

    public void setCustomerId(String customerId) { 
     this.customerId = customerId; 
    } 

    public Date getDateOfBirth() { 
     return dateOfBirth; 
    } 

    public void setDateOfBirth(Date dateOfBirth) { 
     this.dateOfBirth = dateOfBirth; 
    } 

    public String getName() { 
     return name; 
    } 

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

    public String getPhoneNumber() { 
     return phoneNumber; 
    } 

    public Long getId() { 
     return id; 
    } 

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

    public void setPhoneNumber(String phoneNumber) { 
     this.phoneNumber = phoneNumber; 
    } 

    @Override 
    public String toString() { 
     return "Customer [id=" + id + ", name=" + name + ", phoneNumber=" + phoneNumber + "]"; 
    } 

    public Date getUpdatedOn() { 
     return updatedOn; 
    } 

    public void setUpdatedOn(Date updatedOn) { 
     this.updatedOn = updatedOn; 
    } 

    @PrePersist 
     protected void onCreate() { 
     createdDate = new Date(); 
     } 

    @PreUpdate 
     protected void onUpdate() { 
     updatedOn = new Date(); 
     } 



} 

訂單明細

package com.mayank.bitmesra.pojo; 

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 

@Entity 
public class OrderDetails { 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private long id; 

    private String customerId; 

    private String orderId; 

    private String orderName; 

    public String getCustomerId() { 
     return customerId; 
    } 

    public void setCustomerId(String customerId) { 
     this.customerId = customerId; 
    } 

    public String getOrderId() { 
     return orderId; 
    } 

    public void setOrderId(String orderId) { 
     this.orderId = orderId; 
    } 

    public String getOrderName() { 
     return orderName; 
    } 

    public void setOrderName(String orderName) { 
     this.orderName = orderName; 
    } 


} 

OrderDetailsDAOImpl

@Repository 
@Transactional 
public class OrderDetailsDAOImpl implements OrderDetailsDAO{ 

    @PersistenceContext 
    EntityManager entityManager; 


    @Override 
    public List getAllOrderDetails() { 
//  return entityManager.createQuery("Select order from OrderDetails order ").getResultList(); 
    return entityManager.createQuery("Select customer.name from OrderDetails order inner join Customer customer on order.customerId=customer.customerId").getResultList(); 
    } 
+0

我認爲你在hibernate映射和SQL映射之間感到困惑。由於您的實體類使用@Entity進行註釋,因此它們被映射。這也可以使用xml文件來完成。通過映射你的意思是一對一或任何其他關聯。這可以通過添加註釋或xml所需的配置來完成。 – sAm

回答

1

我想你感到困惑。 @實體是HQL用來標識所謂映射實體的註釋。這兩個實體都由它們註釋。

相關問題