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

时间:2013-02-25 21:03:52

标签: 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")

这里是代码:

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
}

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) {
        ...
    }

}

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
}

8 个答案:

答案 0 :(得分:98)

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

@Column(nullable=false)
private Long customerId;

还有:

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

(同样适用于productId / product)。

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

sale.getCustomer().getId()

答案 1 :(得分:51)

如果您遇到遗留数据库,其中某人已经放置了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;

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

答案 2 :(得分:13)

使用它,对我有用:

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

答案 3 :(得分:8)

            //createUserAppointment();

            Sender sender = new Sender("xxxxxxxxxxxxx", 8080, "xxxxx",
                    "xxxx", "Congratulations! You just gave someone a priceless gift - LIFE! Thank you for donating." +
                    "Your next donation date is 13/6/16.", "1", "0", "xxxxxxxxx",
                    "Moja");
            sender.submitMessage();


        }


    });

如果您已经映射了一个列并且意外地在 @JoinColumn 中为名称 referencedColumnName 设置了相同的值,则hibernate会给出相同的愚蠢错误

错误:

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

答案 4 :(得分:1)

希望这会有所帮助!

@OneToOne(optional = false)
    @JoinColumn(name = "department_id", insertable = false, updatable = false)
    @JsonManagedReference
    private Department department;

@JsonIgnore
    public Department getDepartment() {
        return department;
    }

@OneToOne(mappedBy = "department")
private Designation designation;

@JsonIgnore
    public Designation getDesignation() {
        return designation;
    }

答案 5 :(得分:0)

注意只为任何属性提供1个setter和getter。最好的方法是记下所有属性的定义,然后使用eclipse生成setter和getter实用程序而不是手动执行。右键单击选项 - >来源 - >生成Getter和Setter。

答案 6 :(得分:0)

这意味着您在实体类中两次映射了一个列。 举例说明...

    @Column(name = "column1")
    private String object1;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "column1", referencedColumnName = "column1")
    private TableClass object2;

以上代码段中的问题是我们正在重复映射...

解决方案

由于映射是重要的组成部分,因此您不想删除它。相反,您将删除

    @Column(name = "column1")
    private String uniqueId;

您仍然可以通过创建TableClass对象并在其中分配Object1的字符串值来传递object1的值。

这有效100%。我已经使用Postgres和Oracle数据库对此进行了测试。

答案 7 :(得分:0)

我们通过映射Grails 4(GORM)中的子实体而不是父实体,解决了循环依赖关系(父子实体)。

示例:

Class Person {
    String name
}

Class Employee extends Person{
    String empId
}

//Before my code 
Class Address {
    static belongsTo = [person: Person]
}

//We changed our Address class to:
Class Address {
    static belongsTo = [person: Employee]
}
相关问题