我有一张桌子
CREATE TABLE "PEOPLE" (
"NAME" VARCHAR2(40 CHAR) NOT NULL,
"SURNAME" VARCHAR2(40 CHAR) NOT NULL,
"FATHER_NAME" VARCHAR2(40 CHAR),
"IS_RETIRED" CHAR(1 CHAR) DEFAULT 'N' NOT NULL
);
我注释了它:
@Entity
@Table(name = "PEOPLE")
@DynamicInsert
@DynamicUpdate
public class Person {
public enum YesNo {
Y, N;
}
public Person(String name, String surname) {
this.name = name;
this.surname = surname;
}
@Column(name = "NAME", nullable = false, length = 40)
private String name;
@Column(name = "SURNAME", nullable = false, length = 40)
private String surname;
@Column(name = "FATHER_NAME", length = 40)
private String fatherName;
@Enumerated(EnumType.STRING)
@Column(name = "IS_RETIRED", nullable = false)
private YesNo isRetired;
... // simple getter and setter for each field
}
我想运行代码:
...
Peson person = new Person ("John", "Smith");
entityManager.persist(person);
... // diffrent operations, end of old transaction, begin of new
person.setFatherName("Peter");
entityManager.update(person);
我收到了错误:
java.sql.SQLException: ORA-01407: cannot update (???) to NULL
似乎尽管在DB中给出了默认值,但在持久化对象人的退出值之后仍为空。当我尝试对人进行动态更新时,hibernate希望将DB('N')中保存的值更改为null。有没有办法在动态插入后自动更新具有默认值的字段值?是否可以在Java中的类定义中没有硬编码默认值的情况下执行它(如果我在数据库中更改默认值,我不想在Java代码中执行相同操作)?
答案 0 :(得分:0)
查看@DynamicUpdate
的javadoc,似乎它们的关键点是“重新附加分离的实体,如果没有启用select-before-update,这是不可能的。”
所以,我的猜测是它适用于插入,但不适用于更新,因为它是先前事务中的分离实体。