Java 修改){ this.modified=modified; } 公共模板getParent(){ 返回父母; } 公共void setParent(模板父级){ this.parent=parent; this.parent_id=parent.getId(); } 公共CMS区域getRegionId(){ 返回区域ID; } 公共无效设置区域ID(CMS区域区域ID){ this.regionId=regionId; } @凌驾 公共int hashCode(){ int hash=0; hash+=(id!=null?id.hashCode():0); 返回散列; } @凌驾 公共布尔等于(对象){ //TODO:警告-如果未设置id字段,此方法将不起作用 如果(!(模板的对象实例)){ 返回false; } 模板其他=(模板)对象; 返回(this.id!=null | | other.id==null)和&(this.id==null | | this.id.equals(other.id)); } @凌驾 公共字符串toString(){ 返回“com.vw.asa.entities.Templates[id=“+id+”]”; } }

Java 修改){ this.modified=modified; } 公共模板getParent(){ 返回父母; } 公共void setParent(模板父级){ this.parent=parent; this.parent_id=parent.getId(); } 公共CMS区域getRegionId(){ 返回区域ID; } 公共无效设置区域ID(CMS区域区域ID){ this.regionId=regionId; } @凌驾 公共int hashCode(){ int hash=0; hash+=(id!=null?id.hashCode():0); 返回散列; } @凌驾 公共布尔等于(对象){ //TODO:警告-如果未设置id字段,此方法将不起作用 如果(!(模板的对象实例)){ 返回false; } 模板其他=(模板)对象; 返回(this.id!=null | | other.id==null)和&(this.id==null | | this.id.equals(other.id)); } @凌驾 公共字符串toString(){ 返回“com.vw.asa.entities.Templates[id=“+id+”]”; } },java,hibernate,spring-boot,spring-mvc,parent-child,Java,Hibernate,Spring Boot,Spring Mvc,Parent Child,我试图将以下块添加到实体,但它生成了此块后面列出的错误: @JoinColumn(name=“parent\u id”,referencedColumnName=“id”) @ManyToOne(可选=false,fetch=FetchType.LAZY) 私有整数父; 为了补充这一点,我添加了以下getter/setter: public整数getParentId(){ 返回getParent().getId(); } public void setParentId(整数parentId){

我试图将以下块添加到实体,但它生成了此块后面列出的错误:

@JoinColumn(name=“parent\u id”,referencedColumnName=“id”)
@ManyToOne(可选=false,fetch=FetchType.LAZY)
私有整数父;
为了补充这一点,我添加了以下getter/setter:

public整数getParentId(){
返回getParent().getId();
}
public void setParentId(整数parentId){
this.parent_id=parentId;
}
这是编译期间生成的错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.vw.asa.entities.template.TdTemplates column: parent_id (should be mapped with insert="false" update="false")
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.vw.asa.entities.template.TdTemplates column: parent_id (should be mapped with insert="false" update="false")
显然,这与parent_id具有多个定义有关,但是如何构造该实体以便执行以下操作

template.getParent().getId();//返回父\u id
template.setParent(模板);//设置父对象
这里是否有可以删除且冗余的方法/属性

编辑

需要注意的是,我还没有明确定义父子关系如何工作的设计模式


它可能会以最早的记录为父记录,最新的记录为子记录,或者当前/主/活动的记录为父记录,树下的其余记录为子记录。我还没有决定哪种方法是这里最好的方法。

使用以下方法指定可插入和可更新的值

@JoinColumn(referencedColumnName = "id", insertable = false, updatable = false)
这应该可以解决问题