我正在尝试从sql文件插入HSQL数据库。我尝试过这个教程https://antoniogoncalves.org/2014/12/11/generating-database-schemas-with-jpa-2-1/ 它没有从sql文件加载我的行。我不知道我哪里出错了。请帮忙。我的文件是:
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<description>JPA Test</description>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.jpa.entity.Person</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:unittests" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="javax.persistence.sql-load-script-source"
value="import.sql" />
</properties>
</persistence-unit>
import.sql
insert into table Person(person_ID, person_name, age) values(101, "Name1", 30 );
insert into Person(person_ID, person_name, age) values(102, "Name2", 30 );
Person.java
package com.jpa.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="Person")
public class Person {
@Id
@Column(name="person_ID")
private int id;
@Column(name="person_name")
private String name;
@Column(name="age")
private int age;
//generated public getters and setters
DemoJPA.java
package com.jpa.demo;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.jpa.entity.Person;
public class DemoJPA {
private static final EntityManagerFactory emf;
static{
emf = Persistence.createEntityManagerFactory("persistenceUnit");
}
public static EntityManager getEntityManager(){
return emf.createEntityManager();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Persistence.generateSchema("persistenceUnit", null);
EntityManager em = getEntityManager();
em.getTransaction().begin();
Person f = new Person();
f.setId(1);
f.setName("Name1");
f.setAge(29);
em.persist(f);
em.getTransaction().commit();
em.clear();
System.out.println("Done");
}
}
我的文件夹结构是: folderStructure
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jpa.entity</groupId>
<artifactId>JPA2Project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<hibernate.version>4.3.5.Final</hibernate.version>
<slf4j.version>1.7.5</slf4j.version>
<log4j.version>1.7.5</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
</project>
答案 0 :(得分:1)
在>>> isinstance(2, bool)
False
文件中,我在最后的属性标记中添加了以下代码:
persistence.xml
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.create-source" value="metadata"/>
<property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
<property name="javax.persistence.sql-load-script-source" value="import.sql"/>
文件位于import.sql
文件夹中。我们也可以将其放在src/main/resources
文件夹中,并将其引用为META-INF
,
我还使用正确的查询编辑了META-INF/import.sql
。
如果我们需要模型,即@Entity(也称为元数据)来管理数据库模式,则使用此代码。