我有一个使用Spring
和Hibernate
数据库的简单Oracle
应用程序。它具有两个实体(City
和Address
),它们通过外键相互关联(地址实体具有city_id)。当我尝试创建或更新新的City实体时,一切正常,但是当我尝试创建新的Address实体时,出现错误:
ORA-01400: cannot insert NULL into ("SCHEME"."ADDRESS"."ID")
这是来自日志的休眠查询:
SQL: insert into address (is_actual, address, create_date, last_modification_date, city_id) values (?, ?, ?, ?, ?)
很显然,它不会生成新的地址实体的ID ,甚至也不会在查询中添加它。而且我不明白为什么会这样。
这是我的城市班的一部分:
@Entity
@Table(name = "city")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@NamedQuery(name = "findById", query = "select distinct c from City c where c.id = :id")
public class City {
private Long id;
private String name;
private Date createDate;
private Date lastModificationDate;
private Boolean isActual;
private Address address;
@Id
@Column(name = "id", updatable = false, insertable = true)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "id_seq")
@SequenceGenerator(name = "id_seq", sequenceName = "SEQ_CITY", allocationSize = 1)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@OneToOne(mappedBy = "city", fetch = FetchType.LAZY, optional = false, cascade = CascadeType.ALL)
@JsonBackReference
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
.......
这是我的地址课程的一部分
Entity
@Table(name = "address")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Address {
private Long id;
private City city;
private String address;
private Date createDate;
private Date lastModificationDate;
private Boolean isActual;
@Id
@Column(name = "id", updatable = false, insertable = true)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_s")
@SequenceGenerator(name = "id_s", sequenceName = "SEQ_ADDR")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@OneToOne(fetch = FetchType.LAZY, optional = false, cascade = CascadeType.ALL)
@MapsId
@JsonManagedReference
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
.......
我的数据库中有序列SEQ_ADDR
:
CREATE SEQUENCE SEQ_ADDR
START WITH 1
INCREMENT BY 1
NOMAXVALUE;
以下是我的地址创建服务:
@Transactional(propagation = Propagation.REQUIRED)
public void addNewAddress() {
try {
SelectBox selectBox = getComponent("addNewAddressBox", CollapsibleBox.class).getComponent("citiesSelectBox", SelectBox.class);
Long cityId = Long.parseLong(selectBox.getSelectedItems().get(0).getId());
String addressStr = getComponent("addNewAddressBox", CollapsibleBox.class).getComponent("newAddressInput", TextInput.class).getText();
Date today = new Date();
City city = cityDAO.findById(cityId);
Address address = new Address();
address.setAddress(addressStr);
address.setCity(city);
address.setCreateDate(today);
address.setActual(true);
addressDAO.save(address);
} catch(Exception e) {
logger.error("ERROR: CAN'T ADD NEW ADDRESS: " + e.getMessage(), e);
}
}
这是我的SAVE方法:
public Address save(Address address) {
Session currentSession = sessionFactory.getCurrentSession();
currentSession.persist(address);
return address;
}
这是日志文件的一部分:
2019-10-23 08:44:35,182 INFO [stdout] (transport-[8H32M51S31763]-[test-azimuth-app]-[rejecting]-request-processing-[test-app]-thread-11) Hibernate: insert into address (is_actual, address, create_date, last_modification_date, city_id) values (?, ?, ?, ?, ?)
2019-10-23 08:44:35,185 INFO [stdout] (transport-[8H32M51S31763]-[test-azimuth-app]-[rejecting]-request-processing-[test-app]-thread-11) 08:44:35.184 [transport-[8H32M51S31763]-[test-app]-[rejecting]-request-processing-[test-app]-thread-11] ERROR
org.hibernate.engine.jdbc.batch.internal.BatchingBatch - HHH000315: Exception executing batch [java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("SCHEME"."ADDRESS"."ID")
2019-10-23 08:44:35,185 INFO [stdout] (transport-[8H32M51S31763]-[test-app]-[rejecting]-request-processing-[test-app]-thread-11) ], SQL: insert into address (is_actual, address, create_date, last_modification_date, city_id) values (?, ?, ?, ?, ?)
2019-10-23 08:44:35,185 INFO [stdout] (transport-[8H32M51S31763]-[test-azimuth-app]-[rejecting]-request-processing-[test-azimuth-app]-thread-11) 08:44:35.185 [transport-[8H32M51S31763]-[test-app]-[rejecting]-request-processing-[test-app]-thread-11] ERROR
org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ORA-01400: cannot insert NULL into ("SCHEME."ADDRESS"."ID")
很明显,休眠状态会生成错误的查询,并且不会通过序列生成器生成ID:没有诸如 Hibernate之类的消息:在日志中从double 中选择SEQ_ADDR.nextval。您对此问题有什么建议吗?非常感谢!