Здравствуйте. Существует три таблицы. Две из них являются справочниками с полями integer и varchar , третья имеет связь к справочникам многие к одному и одно опле BigDecimal для примера.
Смысл при формировании коллекции на 10'000-100'000 элементов как правильно её импортнуть в базу зная что справочники заранее уже заполнены и актуальны.
Настройкb hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/Base</property>
<property name="connection.username">root</property>
<property name="connection.password">XXXXX</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="default_schema">public</property>
<property name="jdbc.batch_size">40</property>
<property name="format_sql">true</property>
<property name="cache.use_second_level_cache">true</property>
<mapping class="ru.jcross.hibernate01.model.TData"/>
<mapping class="ru.jcross.hibernate01.model.Spr1"/>
<mapping class="ru.jcross.hibernate01.model.Spr2"/>
</session-factory>
</hibernate-configuration>
@MappedSuperclass
public abstract class Ent implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "id")
private Long id;
}
@Entity
@Table(name="SPR1", schema = "public")
public class Spr1 extends Ent{
private static final long serialVersionUID = 5432935465120709184L;
@Basic
@Column(name="CODE", nullable=false, insertable= true, updatable=true, length=5, unique=true)
private String code;
@Entity
@Table(name="TDATA")
public class TData extends Ent{
private static final long serialVersionUID = 7521704886502026809L;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "SPR1_ID", referencedColumnName = "ID")
private Spr1 spr1id;
@ManyToOne(fetch = FetchType.LAZY, cascade =CascadeType.ALL)
@JoinColumn(name = "SPR2_ID", referencedColumnName = "ID")
private Spr2 spr2id;
@Basic
@Column(name = "SUMMA")
private BigDecimal summa;
......
....
public static void importTable(){
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
ObservableList<TData> listTdata = FXCollections.observableArrayList();
for (int i = 1 ;i<10000;i++) {
TData td = new TData();
Random r = new Random();
String str = String.valueOf(r.nextInt((9 - 1) + 1) + 1);
td.setSpr1id(spr1Dao.getByCode("000"+str));
str = String.valueOf(r.nextInt((49 - 20) + 1) + 20);
td.setSpr2id(spr2Dao.getByCode("0"+str));
Double dbl = new Double(r.nextInt((1000000 - 2500) + 1) + 2500);
dbl = dbl/100;
td.setSumma(new BigDecimal(dbl));
listTdata.add(td);
}
int count = 0;
Transaction tx;
tx =session.beginTransaction();
for(TData itr:listTdata ){
session.save(itr);
if(++count%40==0){
session.flush();
session.clear();
}
}
tx.commit();
session.close();
HibernateUtil.shutdown();
}
....
ERROR
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [ru.jcross.hibernate01.model.Spr1#2]
at org.hibernate.engine.internal.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:594)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:284)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:227)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:92)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:647)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:639)
at org.hibernate.engine.spi.CascadingActions$5.cascade(CascadingActions.java:218)
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:379)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:304)
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:146)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:95)
at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:425)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:249)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:678)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:670)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:665)
at ru.jcross.hibernate01.App.importTable(App.java:117)
at ru.jcross.hibernate01.App.main(App.java:90)
... 6 more