Java Hibernate saveOrUpdate,更新需要原始行的所有字段
我正在使用mysql和hibernate插入和更新表中的行。我使用saveOrUpdate调用。现在,当我尝试更新表中的一行时,我得到一个异常。异常声明我的列requestTime不能为null。显然这是真的,因为我已经将column属性设置为NotNull 我可以添加行。但是当用另外两列的值更新它时,我得到了这个异常 我假设在更新时,我需要从表中读取行,并更新整行。这是真的吗?我有点希望hibernate saveOrUpdate能帮我做到这一点。因此,我有一个对象,当我插入新行时,它具有所有列的getter。但是当我更新时,我有一个对象,它只有主键和新列的getterJava Hibernate saveOrUpdate,更新需要原始行的所有字段,java,mysql,hibernate,Java,Mysql,Hibernate,我正在使用mysql和hibernate插入和更新表中的行。我使用saveOrUpdate调用。现在,当我尝试更新表中的一行时,我得到一个异常。异常声明我的列requestTime不能为null。显然这是真的,因为我已经将column属性设置为NotNull 我可以添加行。但是当用另外两列的值更新它时,我得到了这个异常 我假设在更新时,我需要从表中读取行,并更新整行。这是真的吗?我有点希望hibernate saveOrUpdate能帮我做到这一点。因此,我有一个对象,当我插入新行时,它具有所有
Transaction txD;
Session session;
session = currentSession();
txD = session.beginTransaction();
session.saveOrUpdate(dataStore);
txD.commit();
例外情况
749368 [Thread-2] DEBUG org.hibernate.internal.util.EntityPrinter - com.mcruiseon.carpool.concrete.SubscribeProviderConcrete{acceptedTime=Mon Jul 30 03:39:23 UTC 2012, requestTime=null, subscriberIdentityHash=1218553253, requestIdentity=167093126, subscribedProviderHash=-284086361, isAccepted=true}
749375 [Thread-2] DEBUG org.hibernate.SQL - update carPoolSubscribedProvider set subscriberIdentityHash=?, requestTime=?, subscribedProviderHash=?, isAccepted=?, acceptedTime=? where requestIdentity=?
749398 [Thread-2] DEBUG org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Column 'requestTime' cannot be null [n/a]
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'requestTime' cannot be null
从carPoolSubscribedProvider中选择*
+-----------------+------------------------+---------------------+------------------------+------------+--------------+
| requestIdentity | subscriberIdentityHash | requestTime | subscribedProviderHash | isAccepted | acceptedTime |
+-----------------+------------------------+---------------------+------------------------+------------+--------------+
| 167093126 | -284086361 | 2012-07-27 16:13:19 | 1218553253 | 0 | NULL |
+-----------------+------------------------+---------------------+------------------------+------------+--------------+
编辑:
| carPoolSubscribedProvider | CREATE TABLE `carPoolSubscribedProvider` (
`requestIdentity` varchar(50) NOT NULL DEFAULT '',
`subscriberIdentityHash` varchar(100) NOT NULL,
`requestTime` datetime NOT NULL,
`subscribedProviderHash` varchar(100) DEFAULT NULL,
`isAccepted` tinyint(1) DEFAULT '0',
`acceptedTime` datetime DEFAULT NULL,
PRIMARY KEY (`requestIdentity`),
Hibernate无法确定要如何对空值的属性进行树化。在以下情况下,它无法区分:
若要使列永远不成为update语句的一部分,可以在注释中将updatable属性设置为false。根据它定义的文档:如果编写表定义,该列是否包含在持久性提供程序生成的SQL UPDATE语句中将更容易 非空列有两种情况:
private Date requestTime = new Date();
private String subscriberIdentityHash = "someDefaultHashHere";
oUp=带有空字段的更新行& oDb=更新时从数据库中获取的行
public static <T> T updateChanges(T oDb, T oUp) {
try {
java.lang.reflect.Field[] fields = oDb.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if (field.get(oUp) != null) {
field.set(oDb, field.get(oUp));
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return oDb;
}
公共静态T更新更改(T oDb,T oUp){
试一试{
java.lang.reflect.Field[]fields=oDb.getClass().getDeclaredFields();
用于(字段:字段){
字段。setAccessible(true);
if(field.get(oUp)!=null){
field.set(oDb,field.get(oUp));
}
}
}捕获(非法访问例外e){
e、 printStackTrace();
}
返回oDb;
}
更新和插入是否需要不同的代码流?类似于if(update){row=getColumn(primaryKey),appendValues(row,newValues)}else insert的东西?如果更新时为null,我可以让hibernate忽略列值吗?至少我不知道在null的情况下如何忽略它。如果适用,您可以使其永远不成为更新的一部分。谢谢,我已经用表定义编辑了这个问题,我应该将注释放在哪里?
insert="false", update="false"
private Date requestTime = new Date();
private String subscriberIdentityHash = "someDefaultHashHere";
public static <T> T updateChanges(T oDb, T oUp) {
try {
java.lang.reflect.Field[] fields = oDb.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if (field.get(oUp) != null) {
field.set(oDb, field.get(oUp));
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return oDb;
}