Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
Hibernate core 3.1.2
Hibernate Annotations 3.1b8
Mapping documents:
(in annotations)
Code between sessionFactory.openSession() and session.close():
HibernateUtil.beginTransaction();
s.createQuery("delete from User").executeUpdate();
log.info("deleted users");
HibernateUtil.commitTransaction();
Full stack trace of any exception that occurs:
org.hibernate.exception.ConstraintViolationException: could not execute update query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:84)
at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:326)
at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:209)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1146)
at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:94)
at ...
Name and version of the database you are using:
HSQL 1.8.0
Trace of exportschema:
06-03-2006 23:24:10 | WARN | [main] | AnnotationBinder | | | Package not found or wo package-info.java: com.mki.model
[hibernatetool] alter table USER_CONTACT_XREF drop constraint FKD68E8DEE65B4E499;
[hibernatetool] alter table USER_CONTACT_XREF drop constraint FKD68E8DEE7F938110;
[hibernatetool] drop table CONTACT_INFO if exists;
[hibernatetool] drop table USERS if exists;
[hibernatetool] drop table USER_CONTACT_XREF if exists;
[hibernatetool] create table CONTACT_INFO (contact_info_id integer generated by default as identity (start with 1), created_on timestamp not null, last_updated timestamp not null, privacy_level integer not null, value varchar(64) not null, contact_type integer not null, primary key (contact_info_id), unique (contact_type));
[hibernatetool] create table USERS (user_id integer generated by default as identity (start with 1), created_on timestamp not null, last_updated timestamp not null, username varchar(32) not null, type integer not null, first_name varchar(255), middle_name varchar(255), last_name varchar(255), password varchar(64) not null, primary key (user_id), unique (username));
[hibernatetool] create table USER_CONTACT_XREF (user_fk integer not null, contact_info_fk integer not null, primary key (user_fk, contact_info_fk), unique (contact_info_fk));
[hibernatetool] alter table USER_CONTACT_XREF add constraint FKD68E8DEE65B4E499 foreign key (user_fk) references USERS;
[hibernatetool] alter table USER_CONTACT_XREF add constraint FKD68E8DEE7F938110 foreign key (contact_info_fk) references CONTACT_INFO;
[hibernatetool] 2 errors occurred while performing <hbm2ddl>.
[hibernatetool] Error #1: java.sql.SQLException: Table not found: USER_CONTACT_XREF in statement [alter table USER_CONTACT_XREF]
[hibernatetool] Error #1: java.sql.SQLException: Table not found: USER_CONTACT_XREF in statement [alter table USER_CONTACT_XREF]
The generated SQL (show_sql=true): and Hibernate Log excerpt:
06-03-2006 23:12:59 | WARN | [main] | AnnotationBinder | | | Package not found or wo package-info.java: com.mki.model
06-03-2006 23:13:01 | WARN | [main] | Configurator | | | No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/Users/james/projects/mki/lib/run/hibernate/ehcache-1.1.jar!/ehcache-failsafe.xml
06-03-2006 23:13:02 | DEBUG | [main] | HibernateUtil | | | Opening new Session for this thread.
06-03-2006 23:13:02 | DEBUG | [main] | HibernateUtil | | | Starting new database transaction in this thread.
Hibernate: insert into USERS (created_on, last_updated, username, type, first_name, last_name, middle_name, password, user_id) values (?, ?, ?, ?, ?, ?, ?, ?, null)
Hibernate: call identity()
Hibernate: insert into CONTACT_INFO (created_on, last_updated, privacy_level, value, contact_type, contact_info_id) values (?, ?, ?, ?, ?, null)
Hibernate: call identity()
06-03-2006 23:13:02 | DEBUG | [main] | HibernateUtil | | | Committing database transaction of this thread.
Hibernate: insert into USER_CONTACT_XREF (user_fk, contact_info_fk) values (?, ?)
Hibernate: select this_.user_id as user1_0_1_, this_.created_on as created2_0_1_, this_.last_updated as last3_0_1_, this_.username as username0_1_, this_.type as type0_1_, this_.first_name as first6_0_1_, this_.last_name as last7_0_1_, this_.middle_name as middle8_0_1_, this_.password as password0_1_, contactinf2_.user_fk as user1_3_, contactinf3_.contact_info_id as contact2_3_, contactinf3_.contact_info_id as contact1_1_0_, contactinf3_.created_on as created2_1_0_, contactinf3_.last_updated as last3_1_0_, contactinf3_.privacy_level as privacy4_1_0_, contactinf3_.value as value1_0_, contactinf3_.contact_type as contact6_1_0_ from USERS this_ left outer join USER_CONTACT_XREF contactinf2_ on this_.user_id=contactinf2_.user_fk left outer join CONTACT_INFO contactinf3_ on contactinf2_.contact_info_fk=contactinf3_.contact_info_id where this_.username=?
06-03-2006 23:13:02 | DEBUG | [main] | HibernateUtil | | | Closing Session of this thread.
06-03-2006 23:13:02 | DEBUG | [main] | HibernateUtil | | | Opening new Session for this thread.
06-03-2006 23:13:02 | DEBUG | [main] | HibernateUtil | | | Starting new database transaction in this thread.
Hibernate: delete from USERS
06-03-2006 23:13:03 | WARN | [main] | JDBCExceptionReporter | | | SQL Error: -8, SQLState: 23000
06-03-2006 23:13:03 | ERROR | [main] | JDBCExceptionReporter | | | Integrity constraint violation FKD68E8DEE65B4E499 table: USER_CONTACT_XREF in statement [delete from USERS]
06-03-2006 23:13:03 | DEBUG | [main] | HibernateUtil | | | Closing Session of this thread.
So I have a few questions:
1. Why does it say "Package not found or wo package-info.java: com.mki.model" even though I have a com/mki/model/package-info.java? Is there a particular annotation I need on that file?
2. Why can't schemaexport find the USER_CONTACT_XREF table after creating it - or throw some sort of error on trying to create?
3. Why can't I delete * from Users? Cascade is set to all, so it shouldn't violate any non-null or unique constraints.
Here's the relevant java:
@MappedSuperclass
public class Identifiable implements Serializable {
...
@Id
@GeneratedValue
@Column(name = "id")
public Integer getId() {
return id;
}
...
}
@Entity
@Table(name = "USERS")
@AttributeOverride(name = "id", column = @Column(name = "user_id"))
public class User extends Identifiable {
...
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "USER_CONTACT_XREF",
joinColumns = { @JoinColumn(name = "user_fk", referencedColumnName = "user_id") },
inverseJoinColumns = { @JoinColumn(name = "contact_info_fk", referencedColumnName = "contact_info_id") })
Set<ContactInfo> getContactInfo() {
return contactInfo;
}
...
}
@Entity
@Table(name = "CONTACT_INFO")
@AttributeOverride(name = "id", column = @Column(name = "contact_info_id"))
public class ContactInfo extends Identifiable {
...
}
I used a join table for the onetomany because the annotations documentation says, "A unidirectional one to many with join table is much preferred. This association is described through an @JoinTable."
There isn't a lot of documentation on the ejb3 annotations (not the fault of the Hibernate project; it's not their jar), but my intent of the column names is as follows:
-xxx_id is a primary key for xxx
-xxx_fk is a foreign key into the table for xxx
I also find it a bit odd that the generated ddl has "unique (contact_info_fk)" instead of "unique (user_fk, contact_info_fk)" though since it's a one-to-many rather than a many-to-many, this is acceptable.
-J