gpt4 book ai didi

java - 列不存在 - Hibernate ManyToMany 关联类

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

我面临以下问题。想象一下这个数据模型:

enter image description here

如您所见,project_function实体是多对多实体的关联。

这是我的实体类。

个人卡:

@Entity
@Table(name = "personal_card")
@XmlRootElement
public class PersonalCard implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_person")
private Integer idPerson;

/* SOME OTHER ATTRIBUTES */

@OneToMany(cascade = CascadeType.ALL, mappedBy = "projectFunctionPK.personalCard")
private Set<ProjectFunction> projectFunctionSet;

public PersonalCard() {
}

public Integer getIdPerson() {
return idPerson;
}

public void setIdPerson(Integer idPerson) {
this.idPerson = idPerson;
}

@XmlTransient
public Set<ProjectFunction> getProjectFunctionSet() {
return projectFunctionSet;
}

public void setProjectFunctionSet(Set<ProjectFunction> projectFunctionSet) {
this.projectFunctionSet = projectFunctionSet;
}

@Override
public int hashCode() {
int hash = 0;
hash += (idPerson != null ? idPerson.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof PersonalCard)) {
return false;
}
PersonalCard other = (PersonalCard) object;
if ((this.idPerson == null && other.idPerson != null) || (this.idPerson != null && !this.idPerson.equals(other.idPerson))) {
return false;
}
return true;
}

}

项目:

@Entity
@Table(name = "project")
@XmlRootElement
public class Project implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_project")
private Integer idProject;

/* OTHER ATTRIBUTES*/

@OneToMany(cascade = CascadeType.ALL, mappedBy = "projectFunctionPK.project")
private Set<ProjectFunction> projectFunctionSet;

public Project() {
}

public Integer getIdProject() {
return idProject;
}

public void setIdProject(Integer idProject) {
this.idProject = idProject;
}

@XmlTransient
public Set<ProjectFunction> getProjectFunctionSet() {
return projectFunctionSet;
}

public void setProjectFunctionSet(Set<ProjectFunction> projectFunctionSet) {
this.projectFunctionSet = projectFunctionSet;
}

@Override
public int hashCode() {
int hash = 0;
hash += (idProject != null ? idProject.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Project)) {
return false;
}
Project other = (Project) object;
if ((this.idProject == null && other.idProject != null) || (this.idProject != null && !this.idProject.equals(other.idProject))) {
return false;
}
return true;
}

}

项目函数:

@Entity
@Table(name = "project_function")
@XmlRootElement
@AssociationOverrides({
@AssociationOverride(name = "projectFunctionPK.project",
joinColumns = @JoinColumn(name = "id_project", referencedColumnName = "id_project")),
@AssociationOverride(name = "projectFunctionPK.personalCard",
joinColumns = @JoinColumn(name = "id_person", referencedColumnName = "id_person")) })
public class ProjectFunction implements Serializable {
private static final long serialVersionUID = 1L;

protected ProjectFunctionPK projectFunctionPK;

private Date fromd;
private Date tod;

public ProjectFunction() {
this.projectFunctionPK = new ProjectFunctionPK();
}

@EmbeddedId
public ProjectFunctionPK getProjectFunctionPK() {
return projectFunctionPK;
}

public void setProjectFunctionPK(ProjectFunctionPK projectFunctionPK) {
this.projectFunctionPK = projectFunctionPK;
}

@Column(name = "fromd")
@Temporal(TemporalType.DATE)
public Date getFromd() {
return fromd;
}

public void setFromd(Date fromd) {
this.fromd = fromd;
}

@Column(name = "tod")
@Temporal(TemporalType.DATE)
public Date getTod() {
return tod;
}

public void setTod(Date tod) {
this.tod = tod;
}

@Transient
public Project getProject() {
return projectFunctionPK.getProject();
}


public void setProject(Project project) {
this.projectFunctionPK.setProject(project);
}

@Transient
public PersonalCard getPersonalCard() {
return this.projectFunctionPK.getPersonalCard();
}

public void setPersonalCard(PersonalCard personalCard) {
//this.personalCard = personalCard;
this.projectFunctionPK.setPersonalCard(personalCard);
}

@Override
public int hashCode() {
int hash = 0;
hash += (projectFunctionPK != null ? projectFunctionPK.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof ProjectFunction)) {
return false;
}
ProjectFunction other = (ProjectFunction) object;
if ((this.projectFunctionPK == null && other.projectFunctionPK != null) || (this.projectFunctionPK != null && !this.projectFunctionPK.equals(other.projectFunctionPK))) {
return false;
}
return true;
}

}

最后是我的嵌入式主键ProjectFunctionPK:

@Embeddable
public class ProjectFunctionPK implements Serializable {

@ManyToOne
private Project project;
@ManyToOne
private PersonalCard personalCard;

public ProjectFunctionPK() {
}

public Project getProject() {
return project;
}

public void setProject(Project project) {
this.project = project;
}

public PersonalCard getPersonalCard() {
return personalCard;
}

public void setPersonalCard(PersonalCard personalCard) {
this.personalCard = personalCard;
}

@Override
public int hashCode() {
int hash = 3;
hash = 41 * hash + Objects.hashCode(this.project);
hash = 41 * hash + Objects.hashCode(this.personalCard);
return hash;
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ProjectFunctionPK other = (ProjectFunctionPK) obj;
if (!Objects.equals(this.project, other.project)) {
return false;
}
if (!Objects.equals(this.personalCard, other.personalCard)) {
return false;
}
return true;
}

}

首先我保存项目。效果很好。然后我想使用project_function连接它 - 所以我创建project_function并将它们设置为现有项目和personal_card,并在尝试坚持后出现以下错误:

Caused by: org.postgresql.util.PSQLException: ERROR: column projectfun0_.personalcard does not exist
Position: 8
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2198)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1927)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:561)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:419)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:304)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
... 119 more

显然 Hibernate 不知道 projectfun0_.personalcard 。但我不知道为什么。您在实体类中看到任何错误吗?或者错误可能在其他地方?

非常感谢您的所有回答:)

最佳答案

EmbeddedId documentation

Relationship mappings defined within an embedded id class are not supported.

因此,ProjectFunctionPK 应仅包含基本映射,并且实体映射应在实体本身中完成。以下是一些相关帖子

https://stackoverflow.com/a/9760808/4074715
https://stackoverflow.com/a/4692144/4074715

关于java - 列不存在 - Hibernate ManyToMany 关联类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29008148/

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