我是Java EE和JPA的新手。所以我写了下面的测试代码。我的问题是,那个数据库表" Blue"已创建,但jpa实体不在eclipselink-orm.xml中。 通常在创建动态实体时,这些实体存储在xml而不是pojo类中是否正确?以下代码中的错误是什么?:
Main.class:
public class Main {
public static void main(String[] args) {
createTestEntities();
}
public static EntityManagerFactory getEmf() {
HashMap<String, String> connectionProperties;
connectionProperties = new HashMap<String, String>();
connectionProperties.put("eclipselink.jdbc.password", "root");
connectionProperties.put("eclipselink.jdbc.user", "root");
connectionProperties.put("eclipselink.jdbc.driver", "com.mysql.jdbc.Driver");
connectionProperties.put("eclipselink.jdbc.url", "jdbc:mysql://localhost:3306/test");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Test", connectionProperties);
return emf;
}
public static void createTestEntities() {
EntityManagerFactory emf = getEmf();
EntityManager em = emf.createEntityManager();
try {
Session session = JpaHelper.getEntityManager(em).getServerSession();
DynamicClassLoader dcl = DynamicClassLoader.lookup(session);
String tableName = "BLUE";
String objectName = "blue";
Class<?> clazz = dcl.createDynamicClass("com.example." + objectName);
DynamicTypeBuilder builder = null;
builder = new JPADynamicTypeBuilder(clazz, null, tableName);
// Die Attribute werden im Key-Value-Style gesetzt
builder.addDirectMapping("ID", Long.class, "MyID");
builder.addDirectMapping("STRING", String.class, "name");
builder.setPrimaryKeyFields("MyID");
DynamicType type = builder.getType();
DynamicHelper dynamicHelper = new JPADynamicHelper(em);
dynamicHelper.addTypes(true /* create table */, false /*foreign key constraints*/, type);
}
catch(DynamicException e) {
} catch(PersistenceException e) {
} catch(Exception e) {
}
}
}
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
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">
<persistence-unit name="Test">
<mapping-file>META-INF/eclipselink-orm.xml</mapping-file>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
</properties>
</persistence-unit>
</persistence>
的EclipseLink-orm.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.1"
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<package>com.example</package>
<persistence-unit-metadata>
<xml-mapping-metadata-complete />
<exclude-default-mappings />
<persistence-unit-defaults>
<access>VIRTUAL</access>
<access-methods set-method="get" get-method="set" />
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>