gpt4 book ai didi

java - Hibernate 因在 ManyToMany 关联上将完全限定的类名添加到属性名而失败

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:26:32 24 4
gpt4 key购买 nike

我正在尝试使用 ManyToMany 关联将两个对象相互映射,但是由于某种原因,当我使用 mappedBy 属性时,hibernate 似乎对我正在映射的内容感到困惑。我的映射唯一奇怪的地方是关联不是在其中一个条目的主键字段上完成的(尽管该字段是唯一的)。

表格是:

Sequence (
id NUMBER,
reference VARCHAR,
)

Project (
id NUMBER
)

Sequence_Project (
proj_id number references Project(id),
reference varchar references Sequence(reference)
)

对象看起来像(注释在 getter 上,将它们放在字段上以压缩一点):

class Sequence {
@Id
private int id;

private String reference;

@ManyToMany(mappedBy="sequences")
private List<Project> projects;
}

拥有方:

class Project {
@Id
private int id;

@ManyToMany
@JoinTable(name="sequence_project",
joinColumns=@JoinColumn(name="id"),
inverseJoinColumns=@JoinColumn(name="reference",
referencedColumnName="reference"))
private List<Sequence> sequences;
}

这会因 MappingException 而失败:

property-ref [_test_local_entities_Project_sequences] not found on entity [test.local.entities.Project]

它似乎很奇怪地在完全限定的类名前面加上下划线。我怎样才能避免这种情况发生?

编辑:我多玩了这个。更改 mappedBy 属性的名称会引发不同的异常,即:

org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: test.local.entities.Project.sequences

因此注解处理正确,但不知何故属性引用未正确添加到 Hibernate 的内部配置中。

最佳答案

我已经完成了您的问题提出的相同场景。而且,正如预期的那样,我得到了同样的异常(exception)。作为补充任务,我已经完成了相同的场景但是一对多多对一通过使用非主键作为连接列例如引用.我现在明白了

SecondaryTable JoinColumn cannot reference a non primary key

嗯,这会是一个错误吗???好吧,是的(而且您的解决方法工作正常 (+1))。如果您想使用非主键作为主键,您必须确保它是唯一的。也许它解释了为什么 Hibernate 不允许使用非主键作为主键(不知情的用户可能会得到意想不到的行为)。

如果你想使用相同的映射,你可以将你的@ManyToMany 关系拆分成@OneToMany-ManyToOne 通过使用封装,你不需要担心你加入的类

项目

@Entity
public class Project implements Serializable {

@Id
@GeneratedValue
private Integer id;

@OneToMany(mappedBy="project")
private List<ProjectSequence> projectSequenceList = new ArrayList<ProjectSequence>();

@Transient
private List<Sequence> sequenceList = null;

// getters and setters

public void addSequence(Sequence sequence) {
projectSequenceList.add(new ProjectSequence(new ProjectSequence.ProjectSequenceId(id, sequence.getReference())));
}

public List<Sequence> getSequenceList() {
if(sequenceList != null)
return sequenceList;

sequenceList = new ArrayList<Sequence>();
for (ProjectSequence projectSequence : projectSequenceList)
sequenceList.add(projectSequence.getSequence());

return sequenceList;
}

}

顺序

@Entity
public class Sequence implements Serializable {

@Id
private Integer id;
private String reference;

@OneToMany(mappedBy="sequence")
private List<ProjectSequence> projectSequenceList = new ArrayList<ProjectSequence>();

@Transient
private List<Project> projectList = null;

// getters and setters

public void addProject(Project project) {
projectSequenceList.add(new ProjectSequence(new ProjectSequence.ProjectSequenceId(project.getId(), reference)));
}

public List<Project> getProjectList() {
if(projectList != null)
return projectList;

projectList = new ArrayList<Project>();
for (ProjectSequence projectSequence : projectSequenceList)
projectList.add(projectSequence.getProject());

return projectList;
}

}

项目顺序

@Entity
public class ProjectSequence {

@EmbeddedId
private ProjectSequenceId projectSequenceId;

@ManyToOne
@JoinColumn(name="ID", insertable=false, updatable=false)
private Project project;

@ManyToOne
@JoinColumn(name="REFERENCE", referencedColumnName="REFERENCE", insertable=false, updatable=false)
private Sequence sequence;

public ProjectSequence() {}
public ProjectSequence(ProjectSequenceId projectSequenceId) {
this.projectSequenceId = projectSequenceId;
}

// getters and setters

@Embeddable
public static class ProjectSequenceId implements Serializable {

@Column(name="ID", updatable=false)
private Integer projectId;

@Column(name="REFERENCE", updatable=false)
private String reference;

public ProjectSequenceId() {}
public ProjectSequenceId(Integer projectId, String reference) {
this.projectId = projectId;
this.reference = reference;
}

@Override
public boolean equals(Object o) {
if (!(o instanceof ProjectSequenceId))
return false;

final ProjectSequenceId other = (ProjectSequenceId) o;
return new EqualsBuilder().append(getProjectId(), other.getProjectId())
.append(getReference(), other.getReference())
.isEquals();
}

@Override
public int hashCode() {
return new HashCodeBuilder().append(getProjectId())
.append(getReference())
.hashCode();
}

}

}

关于java - Hibernate 因在 ManyToMany 关联上将完全限定的类名添加到属性名而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3637956/

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