2017-08-08 27 views
0

我试图用jpa + hibernate和@SQLInsert注释插入到mysql表中。 (我试图更详细的插入查询,直到我意识到基本的不工作)。豆子低于,正在发生的事情在entityManager.persist(或entityManager.merge),即使我设置了豆的3个值,并记录它们的休眠抱怨CKEY为NULLHibernate @SqlInsert注解从bean中获取空值而不是值

豆:

import java.io.Serializable; 
import java.util.Calendar; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.Table; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import org.hibernate.annotations.SQLInsert; 

@Entity () 
@Table (name = "cachedb") 
@SQLInsert(sql="insert into cachedb (ckey , cvalue , expiry) VALUES (? , ? , ? )") 
public class CacheDb implements Serializable 
{ 

    private static final long serialVersionUID = 1L; 

    @Id () 
    @Column (name = "ckey") 
    private String key; 

    @Column (name = "cvalue") 
    private String value; 

    @Column (name = "expiry") 
    private Calendar expiry; 

    @SuppressWarnings ("unused") 
    private CacheDb() 
    { 
    } 

    public CacheDb(final String _key , final String _value) 
    { 
     this.key = _key; 
     this.value = _value; 
    } 

    public CacheDb(final String _key , final String _value , final int expirtyMinutes) 
    { 
     this.key = _key; 
     this.value = _value; 
     final Calendar cal = Calendar.getInstance(); 
     cal.add(Calendar.MINUTE , expirtyMinutes); 
     this.expiry = cal; 
    } 

    public Calendar getExpiry() 
    { 
     return this.expiry; 
    } 

    public void setExpiry(final Calendar _expiry) 
    { 
     this.expiry = _expiry; 
    } 

    public static long getSerialversionuid() 
    { 
     return serialVersionUID; 
    } 

    public void setKey(final String _key) 
    { 
     this.key = _key; 
    } 

    public String getKey() 
    { 
     return this.key; 
    } 

    public void setIKey(final String _key) 
    { 
     this.key = _key; 
    } 

    public String getValue() 
    { 
     return this.value; 
    } 

    public void setValue(final String _value) 
    { 
     this.value = _value; 
    } 

    @Override 
    public String toString() 
    { 
     return "CacheDb [key=" + this.key + ", value=" + this.value + ", expiry=" + this.expiry + "]"; 
    } 
} 

我用来测试插入一些示例代码:

import java.util.List; 
import javax.persistence.Query; 
import com.database.jpa.EntityUtils; 

public class TestInsert 
{ 
    public static void main(String[] args) throws Exception 
    {  
     javax.persistence.EntityManager em = null; 
     String key = "KEY.TEST.08082017"; 
     try   
     { 
      em = EntityUtils.getEntityManagerWithOutTransaction("RLENTYMGR"); 
      em.getTransaction().begin(); 
      final Query q = em.createQuery("select p from CacheDb p where key = ?1"); 
      q.setParameter(1 , key); 
      final List<CacheDb> resultsList = q.getResultList(); 
      if (resultsList.size()==0) 
      { 
       CacheDb newRecord = new CacheDb(); 
       newRecord.setKey(key); // only required column varchar(100) 
       newRecord.setValue("TESTB"); //varchar(1000) 
       //newRecord.setExpiry(null); not needed default is null     
       em.persist(newRecord); 
       //newRecord = em.merge(newRecord); 
      } 
      em.getTransaction().commit(); 
     } 
     catch(final Exception e) 
     { 
      e.printStackTrace(); 
      if (em!=null) 
      { 
       em.getTransaction().rollback(); 
      } 
     } 
     finally 
     { 
      if (em!=null) {em.close();} 
     } 
    } 




} 

除外:

Caused by: java.sql.BatchUpdateException: Column 'CKEY' cannot be null 
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2055) 
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1467) 
    at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:123) 
+0

修复/回答如下 – user18896654353

回答

0

它看起来似乎hibernate没有看你在@SQLInsert中使用的列的顺序,只使用它自己的顺序 - 你必须首先找到它,在安排了我的工作后,我的作品

相关问题