我在 hibernate 和 JPA 中创建了一个父子类。当我尝试持久化该类时,我得到一个 SQL 异常,指出“无效的列索引”。
这是父类:
@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
任何帮助,将不胜感激。我尝试了一些方法,例如将 insertable 设置为 false,但没有运气。我确实看到了一个问题,提到复合键可能存在问题。当子级仅作为父级的一部分存在时,我真的必须在子级上创建一个唯一的序列列吗?