2012-08-03 146 views
0

我在hibernate和JPA中創建了父類和子類。當我嘗試堅持這個類時,我得到一個SQL異常,聲明「無效列索引」。hibernate集合 - 無效的列索引

這是父類:

@Entity 
@Table(name = "vnd_base_file_format") 
public class VendorBaseFileFormat implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    @Column(name = "base_file_format_id") 
    private int baseFileFormatId; 

    @Column(name = "vendor_id") 
    private int vendorId; 

    @Column(name = "format_name") 
    private String formatName; 

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

    @Column(name = "month_year_format") 
    private String monthYearFormat; 

    @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}) 
    @JoinColumn(name="base_file_format_id", nullable=false) 
    @OrderBy("index") 
    private List<VendorBaseFileDimension> dimensions; 

這是子類:

@Entity 
@Table(name = "vnd_base_file_format_dim") 
public class VendorBaseFileDimension implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    @Column(name = "dimension_id") 
    private int dimensionId; 

    @Column(name = "alternate_name") 
    private String alternateName; 

    @Column(name = "dimension_index") 
    private int index; 

    @Id 
    @ManyToOne 
    @JoinColumn(name="base_file_format_id", nullable=false, insertable=false) 
    private VendorBaseFileFormat format; 

我簡單地創建父類,並添加一個子類到它。當我打電話給entityManager.persist時,我收到以下消息:

Hibernate: insert into vnd_base_file_format (enabled, format_name, month_year_format, vendor_id, base_file_format_id) values (?, ?, ?, ?, ?) 
Hibernate: insert into vnd_base_file_format_dim (alternate_name, dimension_index, base_file_format_id, dimension_id) values (?, ?, ?, ?) 
[21:53:01.159] WARN JDBCExceptionReporter - SQL Error: 17003, SQLState: 99999 
[21:53:01.159] ERROR JDBCExceptionReporter - Invalid column index 

任何幫助,將不勝感激。我已經嘗試了一些東西,如設置爲false,但沒有運氣。我看到有一個問題提到組合鍵可能存在問題。當它只是作爲父項的一部分存在時,是否真的必須在子項上創建唯一的序列列?

回答

0

我發現了另一個有用的解決方案的問題。我將個人id列更改爲複合鍵對象,這似乎適用於我。

,我所需要的主要信息是這篇文章here.

,幫助我找到答案的問題中發現的被張貼here.

0

我可以看到您的orderBy註釋將該字段的名稱作爲「索引」。您應該爲您的其他課程中的索引提供一個getter或嘗試將其更改爲「dimension_index」。希望它能解決問題。

+0

我index'改變字段名''到dimensionIndex',但我還是收到相同的錯誤 – mystafer 2012-08-03 02:48:05

+0

而且我相信你已經爲該列準備了吸氣劑了嗎? – 2012-08-03 02:55:17

+0

是的。我對所有列都有getter和setter,我也將'index'的名字改名爲'dimensionIndex'。 – mystafer 2012-08-03 13:00:59

0

我認爲索引列出現問題是因爲您使用List集合來維持關係。你可以做這樣的事情:

1)更改列表設置在VendorBaseFileFormat類;

2)將@IndexColumn(name =「idx」)添加到您的類的註釋中,並通過此列明確指定將用於保存列表索引。您還應該將此列添加到* vnd_base_file_format_dim *表中。在此tutorial存在使用列表(第8節)的雙向一對多映射的示例。

+0

我嘗試使用_ @ IndexColumn_而不是_ @ OrderBy_,但我得到相同的錯誤 – mystafer 2012-08-03 22:47:03