首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >实体错误的映射中的另一个重复列

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

Stack Overflow用户
提问于 2013-02-26 05:03:52
回答 8查看 257K关注 0票数 135

不管其他人的帖子,我找不到一个解决这个错误的方法,在MacOSX,NetBeans 7.2上使用GlassFish。

代码语言:javascript
复制
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")

代码如下:

Sale.java

代码语言:javascript
复制
@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
}

Customer.java

代码语言:javascript
复制
@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) {
        ...
    }

}

Product.java

代码语言:javascript
复制
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
}
EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2013-02-26 05:10:14

消息很清楚:映射中有一个重复的列。这意味着您将同一数据库列映射了两次。事实上,你有:

代码语言:javascript
复制
@Column(nullable=false)
private Long customerId;

还有:

代码语言:javascript
复制
@ManyToOne(optional=false)
@JoinColumn(name="customerId",referencedColumnName="id_customer")
private Customer customer;

(productId/product也是如此)。

您不应该通过ID引用其他实体,而应该通过直接引用该实体来引用它们。删除customerId字段,它是无用的。并对productId执行相同的操作。如果您想要一个销售的客户ID,您只需要这样做:

代码语言:javascript
复制
sale.getCustomer().getId()
票数 163
EN

Stack Overflow用户

发布于 2014-05-13 19:30:39

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

代码语言:javascript
复制
@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;

这允许您访问关系。但是,要添加/更新关系,您必须通过外键定义的@Column值直接操作外键。这不是一种理想的情况,但如果您遇到这种情况,至少可以定义关系,以便能够成功地使用JPQL。

票数 82
EN

Stack Overflow用户

发布于 2017-06-14 16:13:56

使用这个,对我来说是很有用的:

代码语言:javascript
复制
@Column(name = "candidate_id", nullable=false)
private Long candidate_id;
@ManyToOne(optional=false)
@JoinColumn(name = "candidate_id", insertable=false, updatable=false)
private Candidate candidate;
票数 31
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15076463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档