org.hibernate.MappingException:无法确定类型:java.util.Set,表:USERS,列:[org.hibernate.mapping.Column(invoices

Posted

技术标签:

【中文标题】org.hibernate.MappingException:无法确定类型:java.util.Set,表:USERS,列:[org.hibernate.mapping.Column(invoices)]【英文标题】:org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: USERS, for columns: [org.hibernate.mapping.Column(invoices)] 【发布时间】:2013-03-28 18:35:26 【问题描述】:

我有一个问题,Hibernate 无法确定表 USERS 中的 Set 类型。 我正在尝试通过一对多关系创建表 INVOICES 的外键。一个用户可以生成多张发票。 我的 User.java 如下所示。

@Entity
@Table(name="USERS")
public class User 

    @Id
    @Column(name="User_Id",nullable=false)
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer user_id;


    @Column(name="NAME")
    private String name;

    @Column(name="Address")
    private String address;

    @Column(name="Designation")
    private String designation;


    private Set<Invoice> invoices;

    /*@OneToMany
    @JoinColumn(name="Rec_Invoice_ID", nullable=false)
    private Set<RecurringInvoice> recurring_invoices;*/

我正在尝试使用 INVOICE-ID 作为 USERS 表中的外键。 我正在按照此处给出的说明进行操作 Hibernate: Annotation one-to-many (foreign-key)

    @OneToMany
    @JoinColumn(name="INVOICE_ID", nullable=false)
    public Set<Invoice> getInvoices() 
        return invoices;
    

    public void setInvoices(Set<Invoice> invoices) 
        this.invoices = invoices;
    

/*   public Set<RecurringInvoice> getRecurring_invoices() 
        return recurring_invoices;
    

    public void setRecurring_invoices(Set<RecurringInvoice> recurring_invoices) 
        this.recurring_invoices = recurring_invoices;
    
*/
    // Getters and Setters
    public Integer getUser_id() 
        return user_id;
    

    public void setUser_id(Integer user_id) 
        this.user_id = user_id;
    

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public String getAddress() 
        return address;
    

    public void setAddress(String address) 
        this.address = address;
    

    public String getDesignation() 
        return designation;
    

    public void setDesignation(String designation) 
        this.designation = designation;
    


我的 Invoice.java 如下所示。

@Entity
@Table(name="INVOICES")
public class Invoice 


    private Integer invoice_id;

    @Column(name="Date_Created", nullable=false)
    private Timestamp dateCreated;

    @Column(name="DESCRIPTION")
    private String description;

    @Column(name="Total_Amount")
    private Double totalAmount;

    @Column(name="Tax_Amount")
    private Double taxAmount;

    @Column(name="Due_Date")
    private Timestamp dueDate;

    @Column(name="deleted")
    private boolean deleted;


    private InvoiceItemsDetails invoiceItemsDetails;


    private Client client;

    @OneToOne
    @JoinColumn(name="ID", nullable=false)
    public Client getClient() 
        return client;
    

    public void setClient(Client client) 
        this.client = client;
    

    public Date getDueDate() 
        return dueDate;
    

    public void setDueDate(Timestamp dueDate) 
        this.dueDate = dueDate;
    

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="INVOICE_ID", nullable=false, insertable=false,updatable=false)
    public Integer getInvoice_id() 
        return invoice_id;
    

    public void setInvoice_id(Integer invoice_id) 
        this.invoice_id = invoice_id;
    

    public Date getDateCreated() 
        return dateCreated;
    

    public void setDateCreated(Timestamp dateCreated) 
        this.dateCreated = dateCreated;
    

    public String getDescription() 
        return description;
    

    public void setDescription(String description) 
        this.description = description;
    

    public Double getTotalAmount() 
        return totalAmount;
    

    public void setTotalAmount(Double totalAmount) 
        this.totalAmount = totalAmount;
    

    public Double getTaxAmount() 
        return taxAmount;
    

    public void setTaxAmount(Double taxAmount) 
        this.taxAmount = taxAmount;
    

    public boolean isDeleted() 
        return deleted;
    

    public void setDeleted(boolean deleted) 
        this.deleted = deleted;
    

    @OneToOne
    @JoinColumn(name="Invoice_Item_Detail_id", nullable=false)
    public InvoiceItemsDetails getInvoiceItemsDetails() 
        return invoiceItemsDetails;
    

    public void setInvoiceItemsDetails(InvoiceItemsDetails invoiceItemsDetails) 
        this.invoiceItemsDetails = invoiceItemsDetails;
    



【问题讨论】:

【参考方案1】:

如果我没记错的话,Hibernate 不允许您将注释与字段/getter 一起混合和匹配。如果您的 @Id 注释设置在字段上,则所有映射都应遵循字段。尝试将 @OneToMany @JoinColumn(name="INVOICE_ID", nullable=false)getInvoices() 移动到 private Set&lt;Invoice&gt; invoices; 此模式也应应用于您的 Invoice

【讨论】:

你好 orid,我已经按照你说的做了,但现在我得到了一个新的异常 org.hibernate.MappingException: 实体映射中的重复列:net.impetus.dto.Invoice 列:INVOICE_ID(应该映射为 insert="false" update="false") 那是因为invoice_id 字段也映射到相同的列名:@Column(name="INVOICE_ID"... 将FK 列名更改为@OneToMany @JoinColumn(name="USER_ID", nullable=false) private Set&lt;Invoice&gt; invoices; 之类的名称 很好的答案——解决了我在环境中遇到的同样问题。 谢谢,它帮助我解决了代码中的问题,+1 太棒了。多么迂腐!

以上是关于org.hibernate.MappingException:无法确定类型:java.util.Set,表:USERS,列:[org.hibernate.mapping.Column(invoices的主要内容,如果未能解决你的问题,请参考以下文章