@OneToOne映射JPA:字段列表中的未知列

时间:2015-11-16 17:08:47

标签: hibernate java-ee jpa

我有一个数据库如下

@OneToOne
@JoinColumn(name="loc_id",referencedColumnName="loc_id")
private Location location;

所以我想在Enterprise模型类中添加一个字段:“Location”,这是一个@OneToOne关系。不幸的是,添加此字段时,应用程序不再起作用。

位置的企业类中的代码:

@Entity
@Table(name = "Enterprise")
public class Enterprise implements Serializable {
private int entId;
private int locId;
private String entName;
private int entDescription;

@OneToMany(mappedBy = "enterprise")
private List<SalesOrder> salesOrders;

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ent_id")
public int getEntId() {
    return entId;
}

public void setEntId(int entId) {
    this.entId = entId;
}

@Basic
@Column(name = "loc_id")
public int getLocId() {
    return locId;
}

public void setLocId(int locId) {
    this.locId = locId;
}

@Basic
@Column(name = "ent_name")
public String getEntName() {
    return entName;
}

public void setEntName(String entName) {
    this.entName = entName;
}

@Basic
@Column(name = "ent_description")
public int getEntDescription() {
    return entDescription;
}

public void setEntDescription(int entDescription) {
    this.entDescription = entDescription;
}

//bi-directional one-to-one association to Location
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="loc_id",referencedColumnName="loc_id")
private Location location;


public Location getLocation() {
    return this.location;
}

public void setLocation(Location location) {
    this.location = location;
}

带有吸气剂和吸气剂安置者的位置。

添加此“位置”字段时出现以下错误:

  

PersistenceException:org.hibernate.exception.SQLGrammarException:无法提取ResultSet

     

[...]

     

引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'字段列表'中的未知列'enterprise0_.location'

在我看来,它正在数据库中搜索列“位置”,但此列不在数据库中。它应该使用loc_id列代替!我尝试添加@Transient,但这不起作用。

感谢您的帮助!

  

企业级

private int locId;
private String locCountry;
private int locPostcode;
private String locCity;
private String locStreet;
private int locNumber;
private Integer locSubNumber;


@Override
public String toString() {
    return locStreet + " " + locNumber + ", " + locPostcode  + " " + locCity + ", " + locCountry;
}

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "loc_id")
public int getLocId() {
    return locId;
}

public void setLocId(int locId) {
    this.locId = locId;
}

@Basic
@Column(name = "loc_country")
public String getLocCountry() {
    return locCountry;
}

public void setLocCountry(String locCountry) {
    this.locCountry = locCountry;
}

@Basic
@Column(name = "loc_postcode")
public int getLocPostcode() {
    return locPostcode;
}

public void setLocPostcode(int locPostcode) {
    this.locPostcode = locPostcode;
}

@Basic
@Column(name = "loc_city")
public String getLocCity() {
    return locCity;
}

public void setLocCity(String locCity) {
    this.locCity = locCity;
}

@Basic
@Column(name = "loc_street")
public String getLocStreet() {
    return locStreet;
}

public void setLocStreet(String locStreet) {
    this.locStreet = locStreet;
}

@Basic
@Column(name = "loc_number")
public int getLocNumber() {
    return locNumber;
}

public void setLocNumber(int locNumber) {
    this.locNumber = locNumber;
}

@Basic
@Column(name = "loc_sub_number")
public Integer getLocSubNumber() {
    return locSubNumber;
}

public void setLocSubNumber(Integer locSubNumber) {
    this.locSubNumber = locSubNumber;
}
  

位置等级

prefix + d

1 个答案:

答案 0 :(得分:1)

尝试从Enterprise类中删除int locId,因为您已经声明了Location location,其中包含该属性。

另外,您提到Location-Enterprise是双向的,但未在位置侧定义关系