我试图使用jpa + hibernate和@SQLInsert注释插入到mysql表中。 (我正在尝试一个更精细的插入查询,直到我意识到基本的一个不工作)。 bean在下面,在entityManager.persist(或entityManager.merge)上发生了什么,即使我在bean上设置3个值,并记录它们hibernate抱怨CKEY为NULL
豆子:
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 :(得分:2)
看来hibernate不会查看您在@SQLInsert
中使用的列的顺序。
它只使用自己的订单 - 您必须首先通过让Hibernate为您生成插入语句然后在您的自定义@SQLInsert
中模仿它来找到它。
答案 1 :(得分:0)
作为@ user1889665 stated,hibernate使用其自己的列顺序,如docs中所述:
参数顺序很重要,由Hibernate处理属性的顺序定义。您可以通过启用调试日志记录来查看预期的顺序,因此Hibernate可以打印出用于创建,更新和删除实体的静态SQL。
要查看预期的顺序,请记住不要通过注释或映射文件包括您的自定义SQL,因为它们会覆盖Hibernate生成的静态SQL。
基本上,您需要这样做:
@SQLInsert
logging.level.org.hibernate=DEBUG
myCrudRepository.save(myEntity)
org.hibernate.SQL : insert into MY_TABLE (notMyFirstColumn, myLastColumn, myFirstColumn) values (?, ?, ?)
@SQLInsert
中日志中打印的插入语句中的顺序
@SQLInsert(sql = "INSERT INTO MY_TABLE(notMyFirstColumn, myLastColumn, myFirstColumn) values (?, ?, ?)")