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 に答える