Im Hibernate Buch über Schema Export lesen: http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=02validatingthehibernateenvironmentHibernate 3.6: Schema Export schlägt fehl, autocommit = false?
Ich habe versucht, den Code in Hibernate 3.6, aber das Schema wird nicht erstellt, ausgeführt wird, und aus dem Ausgabeprotokoll unter der Annahme, vielleicht ist es nicht irgendwie verpflichtet. Hier ist die Log-Ausgabe:
105 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
118 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.6.0.Final
119 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
122 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
124 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
165 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
165 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
199 [main] WARN org.hibernate.util.DTDEntityResolver - recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
217 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
245 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.PostgreSQLDialect
291 [main] INFO org.hibernate.cfg.Configuration - Hibernate Validator not found: ignoring
295 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport - Running hbm2ddl schema export
295 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport - exporting generated schema to database
297 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
297 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
297 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
306 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: org.postgresql.Driver at URL: jdbc:postgresql://localhost:5432/hibernate
306 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=sofco, password=****}
346 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport - schema export complete
347 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - cleaning up connection pool: jdbc:postgresql://localhost:5432/hibernate
Hier ist mein Code:
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class User {
private Long id;
private String name;
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* @param args
*/
public static void main(String[] args) {
Configuration config = new Configuration();
config.addAnnotatedClass(User.class);
config.configure();
new SchemaExport(config).create(true, true);
}
}
und hier ist meine Hibernate cfg xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- MOHON DITAMBAHKAN SESUAI PACKAGE DAN URUT ABJAD -->
<session-factory>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.username">sofco</property>
<property name="connection.password">kamalbert</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/hibernate</property>
<property name="show_sql">true</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
Ich frage mich, was ich hier fehlt ..
Grüße,
Albert Kam
wow! Wie habe ich das vermisst! Haha. Es funktioniert jetzt gut, danke Mann. In postgresql 9 scheint das Erstellen eines Tabellenname-Benutzers problematisch zu sein. Nachdem der Klassenname in MstUser geändert wurde, funktioniert es einwandfrei. Hier sind die Protokolle: Drop-Tabelle MstUser Kaskade .. Drop-Sequenz Hibernate_sequence .. create Tabelle MstUser (ID int8 nicht null, Name varchar (255), Primärschlüssel (ID)) .. erstellen Sequenz hibernate_sequence .. 542 [ main] INFO org.hibernate.tool.hbm2ddl.SchemaExport - Schema Export abgeschlossen .. – bertie
"Benutzer" ist ein reserviertes Schlüsselwort in PostgreSQL (und Oracle, wahrscheinlich). Sie müssen Ihren Klassennamen nicht ändern (und das sollten Sie nicht). Zitiere es einfach mit @Entity (table = "' user' "). – jpkrohling
Ich habe gestern gerade auch über diese Anmerkung erfahren. Danke! :) – bertie