Hibernate:如何修复“从A改为B的实例的标识符”?

时间:2015-08-28 05:05:03

标签: java spring hibernate

org.hibernate.HibernateException:实例的标识符 of org.com.Pricing从241改为242

我得到了这个例外。我有一个名为application的实体,它被映射到Application Table。当应用程序持续存在时,一些默认值应该保存在另一个名为Pricing的表中。

该项目是一个动态的Web项目,并且有可能有多少用户可以登录。任何用户都可以创建一个应用程序,当他们点击sav时,实体应该保存在数据库中。

当一个用户正在执行sav时它工作正常......但是当3-4个用户试图同时保存时,它会抛出上述异常。应用程序正在创建,但在持续定价时获得上述异常。

我们正在使用Hibernate 4与Spring集成,而Hibernate模板用于事务。 有没有办法解决这个问题。

@Transactional

public class ApplicationDao extends BaseDao {

         private Application application = null;
/**
 * Bean Injection Start
 * @param applicationDTO
 * @return
 */
public void setApplication(Application application) {
    this.application = application;
}
/**
 * End
 */

@SuppressWarnings("unchecked")
public int persistApplication(ApplicationDTO applicationDTO){

    String appName = null;

    int appId = -1;
    Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();

    Query query = session.createQuery("from Application app where app.APP_NAME=:APP_NAME and app.OWNER_ID=:OWNER_ID");

    query.setParameter("APP_NAME", applicationDTO.getAPP_NAME());

    List<Application> applicationList = query.list();
    for(Application application : applicationList){
        appName = application.getAPP_NAME();
    }
    if(appName ==null || appName ==""){
        appName = "NO_DATA";
    }
    if(appName.equalsIgnoreCase(applicationDTO.getPP_NAME())){
        System.out.println("app Name Already Exist");
        appId = -1;
    }
    else
    {

    try{
         BeanUtils.copyProperties(application, applicationDTO);
         appId=application.getAPP_ID();
     }catch(ConversionException coversionExc){

         coversionExc.printStackTrace();
     }catch(IllegalAccessException illegalAccessExce){

         illegalAccessExce.printStackTrace();
     }catch(InvocationTargetException invocationTargetExce){

         invocationTargetExce.printStackTrace();
     }
    saveOrUpdate(application);
    appId=application.getAPP_ID();
    System.out.println("app id "+appId);
    }
    return appId;
}
    public class BaseDao extends HibernateDaoSupport {

protected void saveOrUpdate(Object entity) {
    getHibernateTemplate().saveOrUpdate(entity);
    getHibernateTemplate().flush();
    }

protected HibernateTemplate getTemplate() {
    return getHibernateTemplate();
    }
}




@Entity
 public class Application{


@Id
@GenericGenerator(name="generator", strategy="increment")
@GeneratedValue(generator="generator")
private int APP_ID;

@Column
private String APP_NAME;

public int getAPP_ID() {
    return APP_ID;
}

public void setAPP_ID(int aPP_ID) {
    APP_ID = aPP_ID;
}

public String getAPP_NAME() {
    return APP_NAME;
}

public void setAPP_NAME(String aPP_NAME) {
    APP_NAME = aPP_NAME;
}

}

@Entity
public class Pricing {


@Id
@GenericGenerator(name="generator", strategy="increment")
@GeneratedValue(generator="generator")
private int Pricing_ID;

@Column
private int APP_ID;

@Column
private double PRICE;

public int getAPP_ID() {
    return APP_ID;
}

public void setAPP_ID(int aPP_ID) {
    APP_ID = aPP_ID;
}

public int getPricing_ID() {
    return Pricing_ID;
}

public void setPricing_ID(int pricing_ID) {
    Pricing_ID = pricing_ID;
}

public double getPRICE() {
    return PRICE;
}

public void setPRICE(double pRICE) {
    PRICE = pRICE;
}

}

以下是Application Context xml文件。

    <property name="dataSource" ref="mySQLDBConnectionDetails"></property>

    <property name="annotatedClasses">
        <list>
            <value>org.com.Application</value>
            <value>org.com.Pricing</value>
        </list>
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>
<tx:annotation-driven proxy-target-class="true"
    transaction-manager="transactionManager" />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="mysessionFactory" />
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
    <property name="sessionFactory" ref="mysessionFactory" />
</bean>

如果有任何需要,也会提供该信息......提前致谢。

1 个答案:

答案 0 :(得分:0)

     BeanUtils.copyProperties(application, applicationDTO);
     appId=application.getAPP_ID();

这里您从DTO更新ID(您的实体的主键)复制属性。

您可以存储密钥,复制属性和恢复。

     Long oldId=application.getId();
     BeanUtils.copyProperties(application, applicationDTO);
     application.setId(oldId);
     appId=application.getAPP_ID();