hibernate collection - invalid column index

I have created a parent and child class in hibernate and JPA. When I attempt to persist the class I get a SQL exception stating "invalid column index".

This is the parent class:

@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;

This is the child class:

@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;

I simply create the parent class and add one child class to it. When I call entityManager.persist I get the following message:

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

Any help would be appreciated. I have tried a few things like setting insertable to false, but no luck. I did see one question out there that mentioned there might be a problem with composite keys. Do I really have to create a unique sequence column on the child when it is only going to exist as part of the parent?

Answers


I can see your orderBy annotation is having name of the field as "index". You should provide one getter for index from your other class or try changing it to "dimension_index". Hopefully it should resolve the issue.


I think that problem with index column occurs because you use List collection for holding relationship. You can do such things:

1) Change List to Set in VendorBaseFileFormat class;

2) Add @IndexColumn(name="idx") annotation to your class and explicitly specify by this column that will be used for holding list index. You should also add this column to *vnd_base_file_format_dim* table. In this tutorial exists example of bidirectional one-to-many mapping using List (section 8).


I found another question with a solution that was helpful. I changed the individual id columns into a composite key object and this seemed to work for me.

The main information that I needed was found in this article here.

The question that helped me figure it out was posted here.


Need Your Help

Using Secondary USB Keyboard in .net

.net windows c#-4.0

I want to read data stream or a key stroke from a secondary USB keyboard to my .net windows application only. The keystrokes should be blocked in all other applications such as word,excel etc.. Is ...

Open Graph Actions IOS

ios facebook opengraph

Trying to post an OPEN GRAPH Facebook Action in my iPhone app.