我正在研究spring mvc,并在2个表之间创建一对多关系。我有2个型号:


ContractHeader 1:很多
合同权利很多:1


ContractHeader.java

@Entity
@Table(name = "CONTRACT_HEADER")
public class ContractHeader {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @OneToMany(mappedBy = "contractHeader", cascade = CascadeType.ALL)
    private List<ContractEntitlement> contractEntitlements;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }


    public List<ContractEntitlement> getContractEntitlements() {
        return contractEntitlements;
    }

    public void setContractEntitlements(List<ContractEntitlement> contractEntitlements) {
        this.contractEntitlements = contractEntitlements;
    }

}


ContractEntitlement.java

@Entity
@Table(name = "CONTRACT_ENTITLEMENT")
public class ContractEntitlement {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @ManyToOne
    @JoinColumn(name = "id")
    private ContractHeader contractHeader;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public ContractHeader getContractHeader() {
        return contractHeader;
    }

    public void setContractHeader(ContractHeader contractHeader) {
        this.contractHeader = contractHeader;
    }

}


在eclipse sts中发布时出现错误:

deploy is failing=Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: org.hibernate.MappingException: Repeated column in mapping for entity: com.at.ccts.model.ContractEntitlement column: id (should be mapped with insert="false" update="false"). Please see server.log for more details.


有任何想法吗?

最佳答案

在您的实体ContractHeader中,您的映射将给出一个包含两列名称为id的表,在@JoinColumn中更改name属性

@ManyToOne
@JoinColumn(name = "idContractHeader")
private ContractHeader contractHeader;

10-05 21:12