标准的Java EE环境:JPA + EJB从JSF豆调用。服务器:GlassFish的3.1.1JPA。交易问题,而从休眠迁移到EclipseLink的
守则,开发,测试和部署Hibernate作为JPA提供者,拒绝与EclipseLink的坚持实体 - 在GlassFish中的默认JPA提供商:
下面是我从JSF豆调用代码:
@Singleton
@Startup
public class SecurityService {
@PersistenceContext(unitName = "unit01")
private EntityManager em;
@TransactionAttribute(value = TransactionAttributeType.REQUIRED)
public String createNewUser(String login) {
UserImpl newUser = new UserImpl();
newUser.setLogin(login);
em.persist(newUser);
//em.flush() ------> works fine when uncommented
DirectoryImpl userHome = new DirectoryImpl();
userHome.setOwner(newUser);
em.persist(userHome); // Throws exception due to FK violation
//
//
至于评论,如果我坚持新用户,这显然是错误后冲洗EntityManager的代码才起作用。在测试其他方法时,我发现只有在关闭服务器时才会将其他更改刷新到数据库。
以上所有发生在一个新的GF安装,纯Java EE的设计没有任何Spring或任何框架。
这里是我的persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="unit01" transaction-type="JTA">
<jta-data-source>jdbc/Unit01DS</jta-data-source>
<properties>
<property name="eclipselink.logging.level" value="FINE" />
</properties>
</persistence-unit>
</persistence>
这是我如何创建数据源:
asadmin create-jdbc-connection-pool --datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource --restype javax.sql.ConnectionPoolDataSource --property "User=u1:Password=xxx:URL=jdbc\:mysql\://localhost/dbname" Unit01DS
asadmin create-jdbc-resource --connectionpoolid Unit01DS jdbc/Unit01DS
这一切,没有其他的配置文件/使用的选项。那么为什么我的代码在Hibernate下工作良好,并且在Eclipselink下表现完全不同?有任何想法吗?
UPDATE
进一步的调查表明,问题就出在实体映射的地方。在我的情况都提到实体(UserImpl
和DirectoryImpl
)从一个根类继承,如下所示:
@Entity
@Table(name = "ob10object")
@Inheritance(strategy = InheritanceType.JOINED)
public class RootObjectImpl implements RootObject {
private Long id;
@Id
@Column(name = "ob10id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return id;
}
//
UserImpl
是根实体的直接子类,并有向其他实体
@Entity
@Table(name = "as10user")
@DiscriminatorValue(value = "AS10")
public class UserImpl extends RootObjectImpl implements User {
private String login;
//
//
没有引用
有点复杂的情况是DirectoryImpl
:
@Entity
@Table(name = "st20directory")
@DiscriminatorValue(value = "ST20")
public class DirectoryImpl extends AbstractStorageNode implements Directory {
// Inside there are also no references to other entities
WHE再AbstractStorageNode
还延伸根对象:
@Entity
@Table(name = "st10storage_node")
public abstract class AbstractStorageNode extends RootObjectImpl implements StorageNode {
private Set<AbstractStorageNode> childNodes;
private StorageNode parentNode;
private User owner;
@OneToMany(mappedBy = "parentNode")
public Set<AbstractStorageNode> getChildNodes() {
return childNodes;
}
@ManyToOne(targetEntity = AbstractStorageNode.class)
@JoinColumn(name="st10parent", nullable = true)
public StorageNode getParentNode() {
return parentNode;
}
@ManyToOne(targetEntity = UserImpl.class)
@JoinColumn(name = "as10id") // this is FK to `UserImpl` table.
public User getOwner() {
return owner;
}
// Setters omitted
现在这里是生成的sql:
// Creating user:
INSERT INTO ob10object (field1, field2, ob10discriminator) VALUES ('v1', 'v2', 'AS10')
SELECT LAST_INSERT_ID()
// Creating Directory:
INSERT INTO ob10object (field1, field2, ob10discriminator) VALUES ('v11', 'v22', 'ST20')
SELECT LAST_INSERT_ID()
INSERT INTO st10storage_node (st10name, as10id, st10parent, ob10id) VALUES ('home', 10, null, 11)
现在我看看会发生什么:的EclipseLink不将数据插入到用户表(as10user),造成FK违反最后一个查询。但我仍然不知道为什么会这样。
更新2
这里是个例外:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`dbname`.`st10storage_node`, CONSTRAINT `F_ST10_AS10` FOREIGN KEY (`as10id`) REFERENCES `as10user` (`ob10id`))
Error Code: 1452
我也有同样的问题。看起来Hibernate JPA持久性提供程序缓存体系结构与EclipseLink不同,但我找不到解决方法。这里有一篇有趣的文章,可能有助于... http://blogs.oracle.com/carolmcdonald/entry/jpa_caching – perissf
@perissf,谢谢你的文章。总是认为缓存是规范背后的事情,如果供应商不违反规范,供应商可以自由使用任何缓存机制。我稍微被我在这里得到的东西嗤之以鼻,我坚信我错过了一些配置选项。 – Osw