Hibernate version:3.1
PROBLEM: Im exporting my schema based on my bean classes ( top-down ), and it is almost perfect except for the fact that the annotation @OnDelete has no effect when used with mysql dialect. It is a simple test program.. here's what I've done:
It is a tipical OneToMany schema:
A classe named book:
@Entity
@Table(name="book")
public class Book
implements Serializable
{
... ...
...
@Id(generate=GeneratorType.AUTO)
@NotNull
public java.lang.Integer getId()
{
return id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn( name="customer_fk" )
@OnDelete(action=OnDeleteAction.CASCADE)
public Customer getCustomer()
{
return this.customer;
}
...
and a class customer:
@Entity
@Table(name="customer")
public class Customer
implements Serializable
{
...
...
@Id(generate=GeneratorType.AUTO)
public Integer getId()
{
return this.id;
}
@OneToMany( mappedBy="customer" )
public List<Book> getBooks()
{
return this.books;
}
...
and, then I export schema like this:
...
try
{
cfg = new AnnotationConfiguration( );
// database connection and dialect settings
cfg.configure( CONFIG_FILE_LOCATION );
cfg.addAnnotatedClass( Book.class );
cfg.addAnnotatedClass( Customer.class );
SchemaExport se = new SchemaExport( cfg );
se.create( true , true );
}
and it generates a schema in DDL like this:
create table book ( id integer not null auto_increment, author varchar(255), available bit not null, title varchar(255), customer_fk integer, primary key (id) ) create table customer ( id integer not null auto_increment, lastname varchar(255), age integer, name varchar(255), primary key (id) ) alter table book add index FK2E3AE9C95247C0 (customer_fk), add constraint FK2E3AE9C95247C0 foreign key (customer_fk) references customer (id)
BUT... it puts no integrity restriction on foreign key constraint, like 'ON DELETE CASCADE' as it should ( because of my @OnDelete annotation in book class ), so, what happens is that the database gets inconsistent everytime a customer is erased from database... and plus, there is no annotation like OnDeleteAction.SETNULL, there is only NO_ACTION or CASCADE.. what happened with SETNULL? Its very important...
Does anybody can help me using some integrity restriction like ON DELETE SET NULL and finding why SchemaExport ignores my @OnDelete annotation ?
Help!
|