Hibernate insert with single column table problem
I'm using Hibe开发者_开发百科rnate with a SQLite database. I have the following class :
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Authority {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int idAuthority;
(...)
I then have a class Author, that extends Authority and add 4 or 5 fields. When I try to save an Author object, Hibernate generates the following request :
Hibernate: insert into Authority values ( )
And sqlite doesn't like that.
If I add a dummy String field, like "private String test" and set this property in the constructor, everything works fine.
I'm quite new to hibernate, so I'm not sure how to proceed. Do you have any idea ?
Edit : As requested, the mapping of the Author class :
@Entity
@Table(name="Authors")
@PrimaryKeyJoinColumn(name="idAuthority")
public class Author extends Authority {
@Column(name = "firstName")
protected String firstName;
@Column(name = "lastName")
protected String lastName;
@Column(name = "alias")
protected String alias;
(...)
}
Edit : As requested (bis), the insert code :
public void save(Object data) {
currentSession.saveOrUpdate(data);
}
Nothing fancy... And to give you more possible leads, here is the database schema :
create table Authority (idAuthority integer, primary key (idAuthority))
create table Authors (alias varchar, firstName varchar,
lastName varchar, idAuthority bigint not null, primary key (idAuthority))
N.B. : in SQLite, an integer that is primary key is automatically set to AUTO-INCREMENT.
The Exception raised is this one :
java.sql.SQLException: near ")": syntax error
The request should be more like : insert into Authority values (NULL) to let SQLite do its auto-increment, not this weird "insert into Authority values ()".
Edit : This is definetely a problem with the SqlLite for Hibernate package. I just tried with Hsqldb, and it gives me a proper query :
insert into Authority (idAuthority) values (default)
(Then it fails too, but for very different reasons :p ).
I'm not sure there is a solution to this problem... other than using a different DB.
If you use InheritanceType.JOINED
your table associated with class Authority must contain column associated with idAuthority and your table associated with class Author must contain column associated with idAuthority that is a foreign key to the primary identifier in table which presents Authority. It's required for table relations accociation
Try Overriding the ID.
@Entity
@Table(name="Authors")
@PrimaryKeyJoinColumn(name="idAuthority")
@AttributeOverride(name="idAuthority", column=@Column(name="id"))
public class Author extends Authority {
@Column(name = "firstName")
protected String firstName;
@Column(name = "lastName")
protected String lastName;
@Column(name = "alias")
protected String alias;
(...)
}
精彩评论