Hibernate OneToMany SELECT 在实体的映射中给出重复的列

Posted

技术标签:

【中文标题】Hibernate OneToMany SELECT 在实体的映射中给出重复的列【英文标题】:Hibernate OneToMany SELECT gives repeated column in mapping for entity 【发布时间】:2020-04-19 09:16:17 【问题描述】:

我需要在 Society 和 BlockFloors Table 之间建立一对多的关系。错误: Invocation of init method failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.example.domain.SocietyBlockFloor column: society_id (should be mapped with insert="false" update="false")

SocietyDetails.java

package com.example.domain;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name="society_details")
public class SocietyDetails 
    public static final long serialVersionUID = 1L;

    @Id
    @Column(name = "society_id")
    private String societyId;

    @Column(name = "society_name")
    private String societyName;

    @Column (name = "society_address")
    private String societyAddress;

    @Column (name = "society_country_details_code")
    private String societyCountryDetailsCode;

    @OneToMany(mappedBy = "societyDetails", cascade = CascadeType.ALL)
    private List<SocietyBlockFloor> societyBlockFloor = new ArrayList<SocietyBlockFloor>();

    @Override
    public String toString() 
        return "Society" +
                "societyId='" + societyId + '\'' +
                ", societyName='" + societyName + '\'' +
                ", societyAddress='" + societyAddress + '\'' +
                ", societyCountryDetailsCode='" + societyCountryDetailsCode + '\'' +
                ", societyBlockFloor='" + societyBlockFloor + '\'' +
                '';
    

    public static long getSerialVersionUID() 
        return serialVersionUID;
    

    public String getSocietyId() 
        return societyId;
    

    public void setSocietyId(String societyId) 
        this.societyId = societyId;
    

    public String getSocietyName() 
        return societyName;
    

    public void setSocietyName(String societyName) 
        this.societyName = societyName;
    

    public String getSocietyAddress() 
        return societyAddress;
    

    public void setSocietyAddress(String societyAddress) 
        this.societyAddress = societyAddress;
    

    public String getSocietyCountryDetailsCode() 
        return societyCountryDetailsCode;
    

    public void setSocietyCountryDetailsCode(String societyCountryDetailsCode) 
        this.societyCountryDetailsCode = societyCountryDetailsCode;
    

    public List<SocietyBlockFloor> getSocietyBlockFloor() 
        return societyBlockFloor;
    

    public void setSocietyBlockFloor(List<SocietyBlockFloor> societyBlockFloor) 
        this.societyBlockFloor = societyBlockFloor;
    

SocietyBlockFloor.java

package com.example.domain;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name="society_block_floor")
public class SocietyBlockFloor implements Serializable 
    public static final long serialVersionUID = 1L;

    @Id
    @Column(name="society_id")
    private String societyBlockFloorId;

    @Column(name="society_block")
    private String societyBlock;

    @Column(name="society_floor")
    private String societyFloor;

    @ManyToOne
    @JoinColumn(name = "society_id", nullable = false)
    private SocietyDetails societyDetails;

    @Override
    public String toString() 
        return "HASocietyBlockFloor" +
                "societyBlockFloorId='" + societyBlockFloorId + '\'' +
                ", societyBlock='" + societyBlock + '\'' +
                ", societyFloor='" + societyFloor + '\'' +
                ", societyDetails='" + societyDetails + '\'' +
                '';
    

    public String getSocietyBlockFloorId() 
        return societyBlockFloorId;
    

    public String getSocietyBlock() 
        return societyBlock;
    

    public void setSocietyBlock(String societyBlock) 
        this.societyBlock = societyBlock;
    

    public String getSocietyFloor() 
        return societyFloor;
    

    public void setSocietyFloor(String societyFloor) 
        this.societyFloor = societyFloor;
    

    public SocietyDetails getSocietyDetails() 
        return societyDetails;
    

    public void setSocietyDetails(SocietyDetails societyDetails) 
        this.societyDetails = societyDetails;
    

DAO 类

package com.example.dao.impl;

@Repository
public class SocietyDetailsDAOImpl extends BaseDAOImpl implements SocietyDetailsDAO 
    @Override
    public List<SocietyDetails> getSocietyDetailsByName(String societyName) throws Exception 
        try 
            String queryString = "from SocietyDetails society JOIN society.societyBlockFloor where society.societyName=:societyName";
            Query query = getSessionFactory().getCurrentSession().createQuery(queryString);
            query.setParameter("societyName", societyName);
            List<SocietyDetails> societyDetails = query.list();
            return societyDetails;
         catch (Exception e) 
            e.printStackTrace();
            throw e;
        
    

我已经尝试更改 SocietyBlockFloor.societyBlockFloorId 的名称,但是在 ON 子句中它仍然使用 (society.societyId = socialBlockFloor.societyId),其中 socialBlockFloor.societyId 不存在。

【问题讨论】:

【参考方案1】:

您在映射中有重复的列。您将同一数据库列映射了两次。你有:

@Id
@Column(name="society_id")
private String societyBlockFloorId;

还有

@ManyToOne
@JoinColumn(name = "society_id", nullable = false)
private SocietyDetails societyDetails;

这似乎没用

@Id
@Column(name="society_id")
private String societyBlockFloorId;

如果你想要socialid,请联系societyDetails.getId();

这在 manyToOne 方面 referencedColumnName = "society_id"

【讨论】:

我已经尝试过了,但是在查询 ON 子句中,它仍然使用 social_id。另外,因为我想要映射到每个社会的街区楼层列表。是否可以使用 Hibernate 或将其映射到 helper.java 的基本解决方案?

以上是关于Hibernate OneToMany SELECT 在实体的映射中给出重复的列的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate - OneToMany 单向映射 - SQLGrammarException

Hibernate @OneToMany 的意外行为

在 Hibernate/JPA 中保存一个带有子对象的对象 - @OneToMany

Hibernate @OneToMany 错误:键 'PRIMARY' 的重复条目 '0'

Hibernate如何正确删除@OneToMany中的孩子?

Hibernate/JPA:获取 OneToMany 关系的一个(第一个)元素