防止不需要的级联表更新

时间:2015-08-07 22:07:49

标签: jpa sql-update cascade openjpa one-to-one

我正在创建我的第一个基于JPA的项目。我的应用程序具有几个具有外键关系的表,以实现完整性。许多相关表是规范化查找表。

考虑在Person上建立fk关系的表Accountaccountid

+-Person----+               
| personid  |               +-Account---+
| accountid*|==============>| accountid |
| ...       |               | ...       |
+-----------+               +-----------+

在前端(JSF)中,我为包含所有帐户的Person列表的新selectOneMenu对象创建了一个表单,并将选定的 accountid 添加到正确插入数据库的新 person

但是,Account表也会自动更新,我找不到阻止这种情况发生的方法。

我的课程注释如下:

@Entity
@Table(name="usr_person")
@NamedQuery(name="Person.findAll", query="SELECT u FROM Person u")
public class Person implements Serializable 
{
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="person_id")
    private int personId;

    //bi-directional one-to-one association to UsrAccount
    @OneToOne
    @JoinColumn(name="account_id",insertable=true,updatable=true)
    private Account usrAccount;

    ...
@Entity
@Table(name="usr_account")
@NamedQuery(name="UsrAccount.findAll", query="SELECT u FROM Account u")
public class Account implements Serializable 
{
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="account_id")
    private int accountId;

    //bi-directional one-to-one association to UsrPerson
    @OneToOne(mappedBy="usrAccount")
    private Person usrPerson;

    ...

前端调用下面的方法,该方法只使用EntityManager的merge()函数来持久保存新的Person对象:

public String savePerson()
{
    try
    {
        em.getTransaction().begin();
        person = em.merge( person );
        em.getTransaction().commit();
    }
    catch (Exception e)
    {
        ...

这会导致UPDATE表中的每个对象Account调用

更新

我从代码中删除了EntityManager.merge()调用,而UPDATE语句仍然存在。不知何故,JSF表单导致支持它的Account对象的自动更新。以下行来自JPA追踪:

17448 <snip> openjpa.jdbc.SQL - <t 1905431636, conn 233456683> executing prepstmnt 1172878998 UPDATE usr_account SET loginname = ?, password = ?, privileges = ? WHERE account_id = ? [params=(String) mvreijn, (String) ba5edce0be3a9b8f6ac9b84c72935192b2289b3a341ad432021256c7144b59f4, (int) 90, (int) 1]
17449 <snip> openjpa.jdbc.SQL - <t 1905431636, conn 233456683> [1 ms] spent
17449 <snip> openjpa.jdbc.SQL - <t 1905431636, conn 233456683> executing prepstmnt 386904456 UPDATE usr_account SET loginname = ?, password = ?, privileges = ? WHERE account_id = ? [params=(String) afolmer, (String) ba5edce0be3a9b8f6ac9b84c72935192b2289b3a341ad432021256c7144b59f4, (int) 90, (int) 2]
17450 <snip> openjpa.jdbc.SQL - <t 1905431636, conn 233456683> [1 ms] spent
17450 <snip> openjpa.jdbc.SQL - <t 1905431636, conn 233456683> executing prepstmnt 1322606395 UPDATE usr_account SET loginname = ?, password = ?, privileges = ? WHERE account_id = ? [params=(String) annuska, (String) ba5edce0be3a9b8f6ac9b84c72935192b2289b3a341ad432021256c7144b59f4, (int) 80, (int) 3]
17451 <snip> openjpa.jdbc.SQL - <t 1905431636, conn 233456683> [1 ms] spent

当我commit()交易时执行这些语句。由于某种原因,查询和后续事务导致JPA相信Account中的所有selectOneMenu个对象都被修改;我该如何防止这种情况?首先逐个拆分它们?

更新2

我的persistence.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="matco">
        <class>matco.model.Changelog</class>
        <class>matco.model.Item</class>
        <class>matco.model.ItemAssignment</class>
        <class>matco.model.ItemMaintenance</class>
        <class>matco.model.LstType</class>
        <class>matco.model.LstColor</class>
        <class>matco.model.LstCondition</class>
        <class>matco.model.LstSize</class>
        <class>matco.model.Team</class>
        <class>matco.model.Account</class>
        <class>matco.model.Person</class>
        <class>matco.model.CatBrand</class>
        <class>matco.model.CatItem</class>
        <class>matco.model.CatPrice</class>
        <class>matco.model.CatSupplier</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/matco" />
            <property name="javax.persistence.jdbc.user" value="****" />
            <property name="javax.persistence.jdbc.password" value="****" />
            <!-- TODO remove this in favor of enhancement -->
            <property name="openjpa.RuntimeUnenhancedClasses" value="supported"/>
            <property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SCHEMA=TRACE, SQL=TRACE"/>
            <property name="openjpa.ConnectionFactoryProperties" value="PrintParameters=true" />
        </properties>
    </persistence-unit>
</persistence>

与此同时,我在支持bean中查询Account对象后,我已将它们分离出来,如下所示:

public List<Account> getUsrAccounts()
{
    List<Account> accts = em.createNamedQuery( "Account.findAll", Account.class ).getResultList();
    for (Account acct : accts)
        em.detach(acct);
    return accts;
}

然后,当我提交新的Person对象时,JPA仅更新链接到新Account的{​​{1}}。即使这是不可取的,我也觉得我在某种程度上误用了JPA。

在JPA中使用不可编辑的查找表的常用方法是什么?

2 个答案:

答案 0 :(得分:1)

尝试添加到@OneToOne映射cascade=CascadeType.REFRESH) 这将指示您的ORM框架仅在刷新时(在读取实体时)级联,并且在尝试仅保存其中一个实体时不会级联更新。

答案 1 :(得分:1)

您的persistence.xml中未启用<property name="openjpa.RuntimeUnenhancedClasses" value="supported"/>。找出另一种增强策略。