简体   繁体   English

自动创建表-Hibernate

[英]Automatic Table creation - Hibernate

I am trying to recreate my Oracle database in SQLite using Hibernate. 我正在尝试使用Hibernate在SQLite中重新创建Oracle数据库。 I have used the hbm2ddl tag value as "create". 我已将hbm2ddl标记值用作“创建”。 Still my SQLite DB is not getting created. 仍然没有创建我的SQLite数据库。 Could someone please help me with this ? 有人可以帮我吗? I have posted below my cfg.xml file 我已经在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>

Try to replace this: 尝试替换为:

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

with: 与:

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

You're missing to write hibernate in the property. 您缺少在属性中编写hibernate

And add this line: 并添加以下行:

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

In case you have entity write them like this: 如果您有实体,请像这样编写它们:

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

I think this is what you need, First the persistence.xml: 我认为这是您需要的,首先是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>

Second is the following class which contains the properties needed by the 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(); 
}
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM