gpt4 book ai didi

java - Hibernate 可嵌入类中的可选字段

转载 作者:行者123 更新时间:2023-12-01 05:22:12 25 4
gpt4 key购买 nike

我有一个可嵌入的类,Address。这个想法是使用 @AttributeOverride(s) 映射到旧数据库模式。但是,我希望这些字段是可选的。例如,并非每条记录都有 STREET_ADDRESS_2。

下面的类可以很好地保留/保存到数据库,但在执行 getAll() 操作时会因“java.sql.SQLSyntaxErrorException:用户缺少权限或未找到对象”而崩溃。具体来说,getStreet2() 方法会导致问题。

@Embeddable
public class Address implements Serializable {

private static final long serialVersionUID = 1L;

private String street1;
private String street2;
private String state;
private String zip;
private String city;
private String phone;

@Column(name = "STREET1", updatable=false, insertable=false)
public String getStreet1() {
return street1;
}

public void setStreet1(String street1) {
this.street1 = street1;
}

//@Basic(optional=true)
@Column(name="STREET2", insertable=false, updatable=false)
@NotFound(action=NotFoundAction.IGNORE)
public String getStreet2() {
return street2;
}

public void setStreet2(String street2) {
this.street2 = street2;
}

@Column(name="STATE", updatable=false, insertable=false)
public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

@Column(name="ZIP", updatable=false, insertable=false)
public String getZip() {
return zip;
}

public void setZip(String zip) {
this.zip = zip;
}

@Column(name="CITY", updatable=false, insertable=false)
public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

@Column(name="PHONE", updatable=false, insertable=false)
public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

@Override
public String toString() {
return "Address [street1=" + street1 + ", street2=" + street2
+ ", state=" + state + ", zip=" + zip + ", city=" + city
+ ", phone=" + phone + "]";
}

使用嵌入类的调用如下所示:

@Embedded
@AttributeOverrides({
@AttributeOverride(name = "street1", column = @Column(name = "PERSON_STREET_1")),
@AttributeOverride(name = "street2", column = @Column(name = "PERSON_STREET_2")),
@AttributeOverride(name = "city", column = @Column(name = "PERSON_CITY")),
@AttributeOverride(name = "state", column = @Column(name = "PERSON_STATE")),
@AttributeOverride(name = "zip", column = @Column(name = "PERSON_ZIP")),
@AttributeOverride(name = "phone", column = @Column(name = "SOLDIER_PHONE")) })
@NotFound(action=NotFoundAction.IGNORE)
public Address getPersonAddress() {
return personAddress;
}

public void setPersonAddress(Address personAddress) {
this.personAddress = personAddress;
}

@Embedded
@AttributeOverrides({
@AttributeOverride(name = "street1", column = @Column(name = "DEPARTMENT_STREET")),
@AttributeOverride(name = "city", column = @Column(name = "DEPARTMENT_CITY")),
@AttributeOverride(name = "state", column = @Column(name = "DEPARTMENT_STATE")),
@AttributeOverride(name = "zip", column = @Column(name = "DEPARTMENT_ZIP")),
@AttributeOverride(name = "phone", column = @Column(name = "UNIT_PHONE")) })
public Address getDepartmentAddress() {
return DepartmentAddress;
}

public void setDepartmentAddress(Address DepartmentAddress) {
this.DepartmentAddress = DepartmentAddress;
}

它似乎爆炸了,因为部门地址在数据库中没有要覆盖的 Street2。

有人可以帮忙吗?

最佳答案

在 JPA 中,不存在仅从 Embeddable 派生属性映射子集的概念。每次在实体中使用@Embedded时,Embeddable的所有属性都会成为具有默认映射的实体的属性。正如您的情况,您可以覆盖属性(以及关联),但不能删除它们。

此外,我不知道 Hibernate 中对此有供应商特定的支持。

关于java - Hibernate 可嵌入类中的可选字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10209302/

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