как исправить исключение гибернации - не изменяйте ссылку на коллекцию с удаленной сиротой - PullRequest
0 голосов
/ 25 марта 2019

Я пытаюсь сделать простой проект, который сканирует данные с сайта онлайн-покупок с помощью веб-драйвера, устанавливает эти значения в сущность и сохраняет эту сущность, называемую «продукт», в базу данных.

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>

и код 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);
        }
    }

Примечание: устанавливаю значения для объекта obj перед вызовом метода saveData (). продукт будет выглядеть следующим образом

[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}

Может кто-нибудь помочь мне решить эту проблему? Заранее спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...