我试图将两个Entity类作为持久性单元的一部分包括在内。我在罐子里使用基本的JPA ...没什么好想的。
这是我的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="GameDataPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<!--<class>com.schroetech.playbook.model.cantstop.persistence.CantStopGameData</class>-->
<!--<class>com.schroetech.playbook.model.common.persistence.GamingSession</class>-->
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/PlayBook"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.password" value="admin123"/>
<property name="eclipselink.logging.level" value="OFF"/>
</properties>
</persistence-unit>
</persistence>
GamingSession实体可以很好地保留,但是当我尝试保留CantStopGameData时,出现以下错误:
java.lang.IllegalArgumentException: Object: com.schroetech.playbook.model.cantstop.persistence.CantStopGameData[ gameId=baf706d7-2a00-4dc0-94e2-09ebd2427d54 ] is not a known entity type.
我还尝试了直接包含类(注释掉了几行),但是效果相同。
以下是GamingSession实体的重要部分(有效的部分):
package com.schroetech.playbook.model.common.persistence;
@Entity
@Table(name = "GAMING_SESSION", schema = "APP")
@XmlRootElement
public class GamingSession implements Serializable {
@Column(name = "NUMBER_OF_PLAYS")
private Integer numberOfPlays;
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "SESSION_ID")
private String sessionId;
@Basic(optional = false)
@Column(name = "GAME_NAME")
private String gameName;
@Lob
@Column(name = "DESCRIPTION")
private String description;
private static final String PERSISTENCE_UNIT = "GameDataPU";
...
public static void persist(GamingSession session) {
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
EntityManager em = emfactory.createEntityManager();
em.getTransaction().begin();
em.persist(session);
em.getTransaction().commit();
}
}
和CantStopGameData(无效的):
package com.schroetech.playbook.model.cantstop.persistence;
@Entity
@Table(name = "CANTSTOP_GAMEDATA", schema = "APP")
@XmlRootElement
public class CantStopGameData implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "GAME_ID")
private String gameId;
@Basic(optional = false)
@Column(name = "SESSION_ID")
private String sessionId;
@Basic(optional = false)
@Column(name = "PLAYER1_ID")
private String player1Id;
@Basic(optional = false)
@Column(name = "PLAYER2_ID")
private String player2Id;
@Column(name = "PLAYER3_ID")
private String player3Id;
@Column(name = "PLAYER4_ID")
private String player4Id;
@Column(name = "PLAYER1_WON")
private Boolean player1Won;
@Column(name = "PLAYER2_WON")
private Boolean player2Won;
@Column(name = "PLAYER3_WON")
private Boolean player3Won;
@Column(name = "PLAYER4_WON")
private Boolean player4Won;
private static String PERSISTENCE_UNIT = "GameDataPU";
...
public static void persist(CantStopGameData gameData) {
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
EntityManager em = emfactory.createEntityManager();
em.getTransaction().begin();
em.persist(gameData);
em.getTransaction().commit();
}
public static void persistAll(Collection<CantStopGameData> gameData) {
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
EntityManager em = emfactory.createEntityManager();
em.getTransaction().begin();
gameData.forEach((singleGameData) -> {
em.persist(singleGameData);
});
em.getTransaction().commit();
}
}