hibernate.order_inserts
のために私は奇妙な問題に直面しています。 ここ予期しない順序でhibernate.order_insertsが動作する方法
@Entity
@Table
@Access(AccessType.FIELD)
public class Employee {
@Column
private String name;
@Id
@SequenceGenerator(name = "EMP_SEQ", sequenceName = "EMP_SEQ", allocationSize = 50)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EMP_SEQ")
@Column
private Long id;
@Column
private String salary;
@Fetch(FetchMode.SELECT)
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL)
private List<Address> address;
}
@Entity
@Access(AccessType.FIELD)
public class Address implements Serializable {
@Column
private String street;
@Id
@SequenceGenerator(name = "ADD_SEQ", sequenceName = "ADD_SEQ", allocationSize = 50)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ADD_SEQ")
@Column(name = "ADDRESSID")
private Long addreessId;
@Fetch(FetchMode.SELECT)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "EMPID")
private Employee employee;
}
がhttp://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/event/def/AbstractFlushingEventListener.html#performExecutions%28org.hibernate.event.EventSource%29 挿入がexecuted.Letsは、例えば言うがある道を注文することになる
<bean id="entityManagerFactory" name="emp"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="emp" />
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.order_updates">true</prop>
<prop key="hibernate.order_inserts">true</prop>
<prop key="hibernate.jdbc.batch_versioned_data">true</prop>
<prop key="hibernate.jdbc.batch_size">30</prop>
<prop key="org.hibernate.flushmode">COMMIT</prop>
<prop key="hibernate.id.new_generator_mappings">false</prop>
</props>
</property>
</bean>
今
ドキュメントにHibernateによると、私のentitymanager
の構成である以下の例を考えてみましょう
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/META-INF/spring/batch-context.xml");
EntityManagerFactory entityManagerFactory = (EntityManagerFactory) applicationContext.getBean("entityManagerFactory");
EntityManager entityManager = entityManagerFactory.createEntityManager();
Employee kumar = entityManager.getReference(Employee.class, 2l);
Address kumarAddress = new Address();
kumarAddress.setStreet("kiev");
kumarAddress.setEmployee(kumar);
Employee pavan = new Employee();
pavan.setName("xyz");
pavan.setSalary("100000");
Address pavanAddress = new Address();
pavanAddress.setStreet("USA");
pavanAddress.setEmployee(pavan);
List<Address> emp2Address = new ArrayList<Address>();
emp2Address.add(pavanAddress);
pavan.setAddress(emp2Address);
entityManager.getTransaction().begin();
entityManager.persist(kumarAddress);
entityManager.persist(pavan);
entityManager.getTransaction().commit();
entityManager.close();
}
kumarAddress
の挿入が最初に実行されるため私はhibernate.order_inserts
を有効にしているので、hibernateは両方のアドレスを一緒に挿入する必要があります。pavanAddress
はまだ挿入されていないpavan
に依存しているので失敗するはずです。
この場合、hibernate orderはどのようにクエリを挿入しますか?上記のコードは正常に動作しますが、私のproject.Hibernateでこの問題に直面しています。子の親が挿入されていなくても、 hibernate.order_inserts
を無効にします。私はこの場合、どのように休止状態が動作するのかを理解しようとしています。
は、残念ながらそれは私のapplication.Itには起きていない得ようとしている子テーブルとバッチそれらを上に挿入トリガー – pppavan
単純です:B1、A1、B2をやっています。 HibernateはB1がA1を最初に必要としていることに気付くので、それに応じてFK障害(A1、B1、B2)を回避します。 – Zeromus