Hi jwenting,
If you have a solution to my problem I will not miss to vote for you :-).
I have the same structure of code as yours except that I have a composite key: the problem is that the @TableGenerator doesn't generate anything.
The feature I need is to keep a history of updated entries, so I opted for a shema like this:
id: for the identifier of the item
revision: for the revision number
Naturally I'll keep in the entry infos like 1.when items were changed 2.by who etc... But the problem occures even in a very simplistic shema: here are my code snippets:
Code:
@MappedSuperclass
@IdClass(ArchiveId.class)
@EntityListeners(ArchivableListener.class)
public class Archivable {
protected long id;
protected long revision;
protected ArchiveEntry entry;
public Archivable() {
entry = new ArchiveEntry();
}
@Id
public long getId() {
return id;
}
@Id
@GeneratedValue(strategy=TABLE, generator = "archivable_rev_gen")
@TableGenerator(name = "archivable_rev_gen", table = "archivable_rev", pkColumnName = "name",
valueColumnName = "rev", pkColumnValue = "archivable_revision", initialValue = 1, allocationSize = 1)
...
}
Here's the archive entry declaration
Code:
@Embeddable
public class ArchiveEntry implements Serializable {
/** @author Zied Hamdi for Into © corporation on 5 juin 07 */
private static final long serialVersionUID = 1;
protected Date startDate, endDate;
protected String note;
protected ArchiveEntry originalEntry;
...
}
The problem is that no table generator is created : look at my log:
Code:
2007-08-01 10:46:32,859 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport.execute(154) - Running hbm2ddl schema export
2007-08-01 10:46:32,859 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.execute(170) - import file not found: /import.sql
2007-08-01 10:46:32,859 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport.execute(179) - exporting generated schema to database
2007-08-01 10:46:32,859 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.execute(303) - drop table Person
2007-08-01 10:46:32,875 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.drop(288) - Unsuccessful: drop table Person
2007-08-01 10:46:32,875 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.drop(289) - 'DROP TABLE' ne peut pas être exécuté sur 'PERSON' parce qu'il n'existe pas.
2007-08-01 10:46:32,875 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.execute(303) - create table Person (revision bigint not null, id bigint not null, startDate timestamp, endDate timestamp, note varchar(255), lastOccurence smallint not null, primary key (revision, id))
2007-08-01 10:46:33,015 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport.execute(196) - schema export complete
here are the other needed files:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="IntoJPA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>fr.into.tests.Archivable</class>
<class>fr.into.tests.ArchiveEntry</class>
<class>fr.into.tests.Person</class>
<!-- <jta-data-source>jdbc/intoDB_DS</jta-data-source> -->
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.DerbyDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.connection.driver_class"
value="org.apache.derby.jdbc.ClientDriver" />
<property name="hibernate.connection.username" value="app" />
<property name="hibernate.connection.password" value="app" />
<property name="hibernate.connection.url"
value="jdbc:derby://localhost:1527/sample" />
<property name="hibernate.show_sql"
value="true" />
<!-- <property name="hibernate.use_sql_comments"
value="true" /> -->
</properties>
</persistence-unit>
</persistence>
Code:
package main;
import java.util.Date;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import fr.into.tests.Person;
public class JPAMain {
public static void main(String[] args) throws NamingException {
// InitialContext context = new InitialContext();
// context.lookup("jdbc/intoDB_DS");
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("IntoJPA");
EntityManager em = emf.createEntityManager();
try {
EntityTransaction transaction = em.getTransaction();
transaction.begin();
Person zied = new Person();
zied.setId( 1 );
zied.getEntry().setNote( "première" );
zied.getEntry().setStartDate( new Date() );
em.persist(zied);
// zied = new Person();
// zied.setId( 1 );
// zied.setRevision( 0 );
// zied.getEntry().setNote( "deuxième" );
// zied.getEntry().setStartDate( new Date() );
// em.persist(zied);
transaction.commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
em.close();
emf.close();
}
}
}
any idea?