我正在尝试做一个简单的项目,该项目使用Web驱动程序从在线购物站点爬网数据,将这些值设置为实体,然后将称为“产品”的实体保存到数据库中。
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
}
Attribute.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>
和将数据保存到db的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()方法之前,请先将值设置为product 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}
有人可以在这里帮助我解决上述问题。预先感谢。