gpt4 book ai didi

java - JPA/Hibernate 如何将一个实体的特定字段加入到另一个实体中

转载 作者:搜寻专家 更新时间:2023-10-30 22:11:34 25 4
gpt4 key购买 nike

假设我们有实体 A 和实体 B,每个实体都有对应的表 A 和 B。

实体 A 是一家酒店,有一个 ID 和几个字段,例如国家、城市、邮政编码等。

实体 B 是一个描述列表,有一个 ID(与实体 A 中的 ID 相同)和另外 2 个字段,语言和描述。

我在我的 DAO 中进行以下查询:

    Query query = getEntityManager().createQuery("FROM " + type.getSimpleName() +
" WHERE City = :cityParam AND Country = :countryParam");
query.setParameter("cityParam", cityParam);
query.setParameter("countryParam", countryNameParam);
query.setMaxResults(numberOfResults);
List<HotelDto> hotelMap = query.getResultList();
ListResponseModel result = new ListResponseModel();
result.setHotelMap(hotelMap);
return result.getHotelMap().size() > 0 ? result : null;

实体 A,酒店实体如下所示:

@Entity
@Table(name = "propertyList", uniqueConstraints =
{ @UniqueConstraint(columnNames = "HotelID")})
public class Hotel implements Serializable, EntityWithId<Long> {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
@Column(name = "HotelID")
private Long hotelId;

@Column(name = "Name")
private String name;

@Column(name = "Address")
private String address;

@Column(name = "City")
private String city;

@Column(name = "StateProvince")
private String stateProvince;

@Column(name = "PostalCode")
private String postalCode;

@Column(name = "Country")
private String country;

@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn(name = "propertyhotelid")
private PropertyDescription propertyDescription;
[...]

实体 B,描述,看起来像这样:

@Entity
@Table(name = "propertyDescriptionList", uniqueConstraints =
{ @UniqueConstraint(columnNames = "HotelID")})
public class PropertyDescription implements Serializable, EntityWithId<Long> {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "HotelID", unique = true, nullable = false)
private Long hotelId;

@Column(name = "PropertyDescription")
private String propertyDescription;

问题是,一旦我获得结果集,单个实体将如下所示:

hotelId: 124125,
name: "Random Hotel",
address: "Some Address",
city: "Shambala",
postalCode: "W2 3NA",
country: "Nevereverland",
-propertyDescription: {
hotelId: 105496,
propertyDescription: "Insert Description here bla bla bla."
}
}

我想得到的是这样的:

hotelId: 124125,
name: "Random Hotel",
address: "Some Address",
city: "Shambala",
postalCode: "W2 3NA",
country: "Nevereverland",
propertyDescription: "Insert Description here bla bla bla."
}

因为我只对 Description 本身感兴趣,它是一个字符串,而不对具有 ID(重复)的整个 Object 感兴趣。

实现此目标的最佳方法是什么?

最佳答案

从你的问题来看,你正在寻找的是一种将两个表映射到一个实体的方法吗?如果是这样,您可以使用@SecondaryTable 执行此操作:字符串“描述”将简单地成为您的酒店实体的一个字段。

@Entity
@Table(name = "propertyList", uniqueConstraints = { @UniqueConstraint(columnNames = "HotelID") })
@SecondaryTable(name = "propertyDescriptionList", pkJoinColumns = @PrimaryKeyJoinColumn(name = "HotelID"), uniqueConstraints = { @UniqueConstraint(columnNames = "HotelID") })
public class Hotel implements Serializable {

@Id
@Column(name = "HotelID")
private Long hotelId;

@Column(name = "PropertyDescription", table = "propertyDescriptionList")
private String propertyDescription;

}

关于java - JPA/Hibernate 如何将一个实体的特定字段加入到另一个实体中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26609724/

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