搜索堆栈溢出后,我来到了这个 Can I use hibernate query language for entities not mapped to a table? 但是最近我做了一个小项目,其中两个实体没有被任何Mapped Annotations映射,但我仍然得到了查询的结果。 所以我很困惑,因为帖子说不可能在非映射实体上查询。
客户
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 :(得分:1)
我觉得你很困惑。 @Entity 是HQL用于标识所谓的映射实体的注释。两个实体都由它们注释。