映射实体错误的另一个重复列

can*_*man 95 hibernate java-ee mappingexception

尽管其他所有人都发布了,但我无法在MacOSX,NetBeans 7.2上找到GlassFish的错误解决方案.

Here the error :
SEVERE: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer
prepare method
SEVERE: Exception while preparing the app
SEVERE: [PersistenceUnit: supmarket] Unable to build EntityManagerFactory

...

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity:
com.supmarket.entity.Sale column: customerId
(should be mapped with insert="false" update="false")
Run Code Online (Sandbox Code Playgroud)

这里的代码:

Sale.java

@Entity
public class Sale {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(nullable=false)
    private Long idFromAgency;

    private float amountSold;

    private String agency;

    @Temporal(javax.persistence.TemporalType.DATE)
    private Date createdate;

    @Column(nullable=false)
    private Long productId;

    @Column(nullable=false)
    private Long customerId;

    @ManyToOne(optional=false)
    @JoinColumn(name="productId",referencedColumnName="id_product")
    private Product product;

    @ManyToOne(optional=false)
    @JoinColumn(name="customerId",referencedColumnName="id_customer")
    private Customer customer;


    public void Sale(){}    
    public void Sale(Long idFromAgency, float amountSold, String agency
            , Date createDate, Long productId, Long customerId){        
        ...
    }

    // then getters/setters
}
Run Code Online (Sandbox Code Playgroud)

Customer.java

@Entity
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id_customer")
    private Long id_customer;

    @Column(nullable=false)
    private Long idFromAgency;

    private String  gender,
                    maritalState,
                    firstname,
                    lastname,
                    incomeLevel;

    @OneToMany(mappedBy="customer",targetEntity=Sale.class, fetch=FetchType.EAGER)
    private Collection sales;


    public void Customer(){}

    public void Customer(Long idFromAgency, String gender, String maritalState,
            String firstname, String lastname, String incomeLevel) {
        ...
    }

}
Run Code Online (Sandbox Code Playgroud)

Product.java

public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id_product")
    private Long id_product;

    @Column(nullable=false)
    private Long idFromAgency;

    private String name;

    @OneToMany(mappedBy="product",targetEntity=Sale.class, fetch=FetchType.EAGER)
    private Collection sales;

    //constructors + getters +setters
}
Run Code Online (Sandbox Code Playgroud)

JB *_*zet 111

消息很明确:映射中有重复的列.这意味着您将相同的数据库列映射两次.事实上,你有:

@Column(nullable=false)
private Long customerId;
Run Code Online (Sandbox Code Playgroud)

并且:

@ManyToOne(optional=false)
@JoinColumn(name="customerId",referencedColumnName="id_customer")
private Customer customer;
Run Code Online (Sandbox Code Playgroud)

(同样适用于productId/ product).

您不应该通过其ID引用其他实体,而应通过直接引用该实体.删除该customerId字段,它没用.并做同样的事情productId.如果您想要销售的客户ID,您只需要这样做:

sale.getCustomer().getId()
Run Code Online (Sandbox Code Playgroud)

  • 如何处理`customerId`和`Customer`类的另一个字段之间有`@ EmbeddedId`复合键的情况?在这种情况下,我需要在映射中重复列,我是对的吗? (5认同)
  • 我得到同样的错误,但我的情况有点不同。我的实体可以是一个或多个相同类型实体的父亲。孩子们可以参考他们父亲的 id 以及他们自己的唯一 id。如何解决这种循环依赖? (2认同)
  • @Serban用代码问自己的问题. (2认同)
  • `客户客户= entityManager.getReference(customerId, Customer.class); 销售.setCustomer(客户);` (2认同)
  • @louisamoros是的,你重复一遍,但你添加了`@MapsId("customerId")`,请参阅/sf/ask/1174253881/ (2认同)

小智 59

如果您遇到遗留数据库,其中有人已经放置了JPA注释,但没有定义关系,而您现在正在尝试定义它们以便在您的代码中使用,那么您可能无法删除customerId @Column,因为其他代码可能已经直接引用它.在这种情况下,定义关系如下:

@ManyToOne(optional=false)
@JoinColumn(name="productId",referencedColumnName="id_product", insertable=false, updatable=false)
private Product product;

@ManyToOne(optional=false)
@JoinColumn(name="customerId",referencedColumnName="id_customer", insertable=false, updatable=false)
private Customer customer;
Run Code Online (Sandbox Code Playgroud)

这允许您访问关系.但是,要添加/更新关系,您将直接通过其定义的@Column值操作外键.这不是一个理想的情况,但如果您处于这种情况,至少可以定义关系,以便您可以成功使用JPQL.


小智 17

用这个,对我有用:

@Column(name = "candidate_id", nullable=false)
private Long candidate_id;
@ManyToOne(optional=false)
@JoinColumn(name = "candidate_id", insertable=false, updatable=false)
private Candidate candidate;
Run Code Online (Sandbox Code Playgroud)


dbo*_*bow 9

@Id
@Column(name = "COLUMN_NAME", nullable = false)
public Long getId() {
    return id;
}

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = SomeCustomEntity.class)
@JoinColumn(name = "COLUMN_NAME", referencedColumnName = "COLUMN_NAME", nullable = false, updatable = false, insertable = false)
@org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.ALL)
public List<SomeCustomEntity> getAbschreibareAustattungen() {
    return abschreibareAustattungen;
}
Run Code Online (Sandbox Code Playgroud)

如果你已经映射了一个列并且意外地为@JoinColumn中的namereferencedColumnName设置了相同的值,那么 hibernate给出了同样的愚蠢错误

错误:

引起:org.hibernate.MappingException:实体映射中的重复列:com.testtest.SomeCustomEntity列:COLUMN_NAME(应使用insert ="false"update ="false"映射)

  • 对我来说,这里的一个关键点是错误说“应该用 insert=false update=false 映射”,但实际的参数/方法应该是“insertable=false,updatable=false”。 (2认同)