自动创建表 - Hibernate

时间:2016-02-01 02:20:24

标签: java hibernate sqlite

我正在尝试使用Hibernate在SQLite中重新创建我的Oracle数据库。我已将hbm2ddl标记值用作“create”。我的SQLite DB仍未创建。有人可以帮我这个吗?我已在我的cfg.xml文件下面发布了

<!-- language: xml -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class"> org.sqlite.JDBC  </property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.username"></property>
<property name="hibernate.dialect"> org.hibernate.dialect.SQLiteDialect </property>
<property name="connection.url">jdbc:sqlite:resources/newdb.db</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.order_updates">true</property>
<property name="hibernate.order_inserts">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>

2 个答案:

答案 0 :(得分:0)

尝试替换它:

  <property name="connection.url">jdbc:sqlite:resources/newdb.db</property>

使用:

   <property name="hibernate.connection.url">jdbc:sqlite:resources/newdb.db</property>

您错过了在房产中写hibernate

并添加以下行:

  <property name="current_session_context_class">thread</property>

如果你有实体这样写:

  <mapping class="path.to.your.Entity"/>

答案 1 :(得分:0)

我认为这就是你需要的, 首先是persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence-unit name="foo-persist" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.this.is.entity.foo</class>
    <class>com.this.is.entity.foo</class>
    <class>com.this.is.entity.foo</class>
    <jta-data-source>foo-persist</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>  
    </properties>
</persistence-unit>

第二个是以下类,它包含持久性单元所需的属性:

import java.nio.file.Files; 
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class CommonService { 
EntityManager em;
public static final String PERSISTENT_UNIT = "foo-persist"; 
EntityManagerFactory emf = Persistence.createEntityManagerFactory(Config.PERSISTENT_UNIT);
Map properties = new HashMap();

public Map getProperties() {
    return properties;
}

public EntityManager getEntityManager()  {

    properties.put("javax.persistence.jdbc.url", "");
    properties.put("javax.persistence.jdbc.user", "");        
    properties.put("javax.persistence.jdbc.password", "");
    properties.put("javax.persistence.jdbc.driver", "org.sqlite.JDBC");
    properties.put("eclipselink.logging.level", "OFF");
    properties.put("javax.persistence.schema-generation.database.action", "create");
    properties.put("javax.persistence.schema-generation.create-script-source", "META-INF/script.sql");
    properties.put("javax.persistence.sql-load-script-source", "META-INF/script.sql");
    emf = Persistence.createEntityManagerFactory(Config.PERSISTENT_UNIT, properties);
    return em = (EntityManager) emf.createEntityManager(); 
}
 }