0

我做了一个程序,如果数据库字段中的值不同,它将保存数据,如果数据库字段中的值相同,它将更新数据,

我坚持让“休眠”可以检查数据库中的值

任何帮助都会很高兴提前感谢

现在我可以为休眠提供条件以检查数据是否存在......当条件为真时,此代码有效,但当条件为假时,我得到了一些错误......

更新我的代码

            Query query = session.createQuery("from CoreService where MODULE_NAME= :lolo1 and OBJECT_NAME= :lolo2 and SYSTEM_ID= :lolo3 and LOG_DATE= :lolo4");
            query.setParameter("lolo1", module);
            query.setParameter("lolo2", object);
            query.setParameter("lolo3", system_id);
            query.setParameter("lolo4", log_date);

            @SuppressWarnings("unchecked")
            List<CoreService> result = query.list();
            if(result.isEmpty()){
                System.out.println("LOLOH");

                CoreService data = new CoreService();
                data.setMODULE_NAME(module);
                data.setCATEGORY("SYSTEMID");
                data.setSYSTEM_ID(system_id);
                data.setCOUNTER_SYSTEM(1);
                data.setOBJECT_NAME(object);
                data.setCOUNTER_SERVICE(1);
                data.setLOG_DATE(log_date);
                data.setUPDATE_LOG(update_log);

                session.beginTransaction();
                session.save(data);
                session.getTransaction().commit();

            }else{
                System.out.println("LELEH");

                CoreService data = new CoreService();

                data.setCATEGORY("SYSTEMID");

                data.setCOUNTER_SYSTEM(2);

                data.setCOUNTER_SERVICE(2);



                session.beginTransaction();
                session.update(data);
                session.getTransaction().commit();

            }

这里有错误~

org.hibernate.TransientObjectException: The given object has a null identifier: id.co.bni.coreservice.model.CoreService
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.getUpdateId(DefaultSaveOrUpdateEventListener.java:272)
at org.hibernate.event.def.DefaultUpdateEventListener.getUpdateId(DefaultUpdateEventListener.java:72)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:240)
at org.hibernate.event.def.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:57)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireUpdate(SessionImpl.java:742)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:722)
at id.co.bni.coreservice.controller.CoreServiceController.LoadData(CoreServiceController.java:271)
at id.co.bni.coreservice.controller.CoreServiceController.actionPerformed(CoreServiceController.java:167)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

这是我的班级模型~(注意:我的 ID 是自动递增的)

package id.co.bni.coreservice.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;


@Entity
@Table(name = "coreservice")
public class CoreService {

private Integer ID;
private String MODULE_NAME;
private String CATEGORY;
private String SYSTEM_ID;
private Integer COUNTER_SYSTEM;
private String OBJECT_NAME;
private Integer COUNTER_SERVICE;
private Date LOG_DATE;
private Date UPDATE_LOG;

/**
 * @return
 */

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID", unique=true, nullable=false)
public Integer getID() {
    return ID;
}

public void setID(Integer ID) {
    this.ID = ID;
}

@Column(name = "MODULE_NAME")
public String getMODULE_NAME() {
    return MODULE_NAME;
}

public void setMODULE_NAME(String MODULE_NAME) {
    this.MODULE_NAME = MODULE_NAME;
}

@Column(name = "CATEGORY")
public String getCATEGORY() {
    return CATEGORY;
}

public void setCATEGORY(String CATEGORY) {
    this.CATEGORY = CATEGORY;
}

@Column(name = "SYSTEM_ID")
public String getSYSTEM_ID() {
    return SYSTEM_ID;
}

public void setSYSTEM_ID(String SYSTEM_ID) {
    this.SYSTEM_ID = SYSTEM_ID;
}

@Column(name = "COUNTER_SYSTEM")
public Integer getCOUNTER_SYSTEM() {
    return COUNTER_SYSTEM;
}

public void setCOUNTER_SYSTEM(Integer COUNTER_SYSTEM) {
    this.COUNTER_SYSTEM = COUNTER_SYSTEM;
}

@Column(name = "OBJECT_NAME")
public String getOBJECT_NAME() {
    return OBJECT_NAME;
}

public void setOBJECT_NAME(String OBJECT_NAME) {
    this.OBJECT_NAME = OBJECT_NAME;
}

@Column(name = "COUNTER_SERVICE")
public Integer getCOUNTER_SERVICE() {
    return COUNTER_SERVICE;
}

public void setCOUNTER_SERVICE(Integer COUNTER_SERVICE) {
    this.COUNTER_SERVICE = COUNTER_SERVICE;
}

@Temporal(TemporalType.DATE)
@Column(name = "LOG_DATE")
public Date getLOG_DATE() {
    return LOG_DATE;
}

public void setLOG_DATE(Date LOG_DATE) {
    this.LOG_DATE = LOG_DATE;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "UPDATE_LOG")
public Date getUPDATE_LOG() {
    return UPDATE_LOG;
}

public void setUPDATE_LOG(Date UPDATE_LOG) {
    this.UPDATE_LOG = UPDATE_LOG;
}


/**
 * @param args
 */
}
4

1 回答 1

0

在 Else 块中,您必须从结果中获取数据,然后设置所需的值。

else{
                System.out.println("LELEH");

                CoreService data = (CoreService )result.get(0);

                data.setCATEGORY("SYSTEMID");

                data.setCOUNTER_SYSTEM(2);

                data.setCOUNTER_SERVICE(2);



                session.beginTransaction();
                session.update(data);
                session.getTransaction().commit();

            }
于 2013-10-09T08:34:42.260 回答