gpt4 book ai didi

java - 是否可以对非映射实体使用 HQL

转载 作者:行者123 更新时间:2023-12-01 09:00:24 26 4
gpt4 key购买 nike

搜索堆栈溢出后我发现了这个
Can I use hibernate query language for entities not mapped to a table?但最近我正在制作一个小项目,其中两个实体没有被任何映射注释映射,但我仍然得到了查询结果。所以我很困惑,因为帖子说无法查询非映射实体。

客户

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;
}


}

订单详细信息DAOImpl

@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();
}

最佳答案

我认为你很困惑。 @Entity 是 HQL 用于标识所谓的映射实体的注释。这两个实体都由它们注释。

关于java - 是否可以对非映射实体使用 HQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41719060/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com