java - 如何修复休眠异常 - 不要更改对启用了删除孤儿的集合的引用
问题描述
我正在尝试做一个简单的项目,该项目使用网络驱动程序从在线购物网站抓取数据,将这些值设置为实体并将名为“产品”的实体保存到数据库中。
Hibernate 抛出以下异常
at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357) ~[?:1.8.0_151]
at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895) ~[?:1.8.0_151]
... 6 more
Caused by: javax.persistence.PersistenceException: org.hibernate.HibernateException: Don't change the reference to a collection with delete-orphan enabled : com.model.Product.attributes
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154) ~[hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181) ~[hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188) ~[hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1490) ~[hibernate-core-5.4.1.Final.jar:5.4.1.Final]
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:515) ~[hibernate-core-5.4.1.Final.jar:5.4.1.Final]
这是 Product.java 实体类
public class Product {
private Integer id;
private String name;
private String category;
private String productId;
private String url;
private List<Attribute> attributes = new ArrayList<Attribute>(0);
public Product() {}
// setters and getters
}
属性.java
public class Attribute {
private String label;
private String value;
private Integer id;
private Date createdDate;
public Attribute(String label, String value, Integer id, Date createdDate) {
this.label = label;
this.value = value;
this.id = id;
this.createdDate = createdDate;
}
public Attribute() {}
// setters and getters
}
.hbm 文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
"classpath:///hibernate-mapping-3.0.dtd">
<hibernate-mapping default-cascade="all-delete-orphan">
<class name="com.model.Product" table="product">
<id name="id" type="integer" column="ID">
<generator class="native" />
</id>
<property name="productId" type="java.lang.String" />
<property name="name" type="java.lang.String" />
<property name="category" type="java.lang.String" />
<property name="url" type="string" />
<list name="attributes" inverse="true"
cascade="all-delete-orphan">
<key column="attributes_Product" />
<list-index column="SORT_ORDER" />
<one-to-many class="com.model.Attribute" />
</list>
</class>
<class name="com.model.Attribute" table="attribute">
<id name="id" type="integer" column="ID">
<generator class="native" />
</id>
<discriminator column="TYPE" type="string" />
<property name="createdDate" type="timestamp" />
<property name="label" type="java.lang.String" />
<property name="value" type="text" />
</class>
</hibernate-mapping>
cfg 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "classpath:///hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/testdb</property>
<property name="connection.username">root</property>
<property name="connection.password">dev</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.show_sql">false</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.order_updates">true</property>
<property name="hibernate.order_inserts">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<mapping resource="model.hbm.xml" />
</session-factory>
</hibernate-configuration>
以及将数据保存到数据库中的java代码
@Override
public synchronized void saveData(Product data) {
Session session = sessionFactory.openSession();
Transaction txn = null;
try {
txn = session.beginTransaction();
session.save(data);
txn.commit();
} catch (HibernateException e) {
LOG.error("HibernateException :", e);
} finally {
HibernateFactory.close(session);
}
}
注意:在调用 saveData() 方法之前将值设置为产品 obj。产品 obj 如下所示
[pool-2-thread-3] EntityPrinter - com.model.Product{ productId=men-cotton-plain-charcoal-shorts, name=Men Cotton Plain Charcoal Shorts, attributes=[], id=1, category=shorts, url=null}
这是日志:
[DEBUG] 2019-03-25 16:00:38.413 [pool-2-thread-3] TransactionImpl - begin
[DEBUG] 2019-03-25 16:00:38.429 [pool-2-thread-3] ActionQueue - Executing identity-insert immediately
[DEBUG] 2019-03-25 16:00:38.433 [pool-2-thread-3] SQL -
insert
into
product
(productId, name, category, url)
values
(?, ?, ?, ?)
[DEBUG] 2019-03-25 16:00:38.481 [pool-2-thread-3] SQL -
select
last_insert_id()
[DEBUG] 2019-03-25 16:00:38.485 [pool-2-thread-3] IdentifierGeneratorHelper - Natively generated identity: 1
[DEBUG] 2019-03-25 16:00:38.485 [pool-2-thread-3] ResourceRegistryStandardImpl - HHH000387: ResultSet's statement was not registered
[DEBUG] 2019-03-25 16:00:38.488 [pool-2-thread-3] TransactionImpl - committing
[DEBUG] 2019-03-25 16:00:38.488 [pool-2-thread-3] AbstractFlushingEventListener - Processing flush-time cascades
[DEBUG] 2019-03-25 16:00:38.489 [pool-2-thread-3] AbstractFlushingEventListener - Dirty checking collections
[DEBUG] 2019-03-25 16:00:38.492 [pool-2-thread-3] Collections - Collection found: [com.model.Product.attributes#1], was: [<unreferenced>] (initialized)
[DEBUG] 2019-03-25 16:00:38.493 [pool-2-thread-3] AbstractFlushingEventListener - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
[DEBUG] 2019-03-25 16:00:38.493 [pool-2-thread-3] AbstractFlushingEventListener - Flushed: 2 (re)creations, 0 updates, 0 removals to 2 collections
[DEBUG] 2019-03-25 16:00:38.494 [pool-2-thread-3] EntityPrinter - Listing entities:
[DEBUG] 2019-03-25 16:00:38.494 [pool-2-thread-3] EntityPrinter - com.model.Product{productId=men-cotton-plain-charcoal-shorts, name=Men Cotton Plain Charcoal Shorts, attributes=[], id=1, category=shorts, url=null}
有人可以在这里帮我解决上述问题。提前致谢。
解决方案
推荐阅读
- javascript - 在方法对象上循环 Pomise
- c++ - C++ MFC StretchDIBits PixelFormat8bppIndexed
- java - 如何将我给定的字符串转换为日期格式?
- php - php.ini request_order 安全问题
- c# - DefaultCellStyle 不适用
- command - 如何使用命令中心重命名动态输出
- pdf - Google 数据洞察导出为 PDF 和预定的电子邮件
- mongodb - MongoDB:文档何时更新
- verilog - 在 SystemVerilog 中检查 NaN 或 Inf
- mysql - 如何对查询中的计算列进行分组?