現在、Spring MVC、Spring data(JPA)を使った開発をしています。
HSQLDB(メモリ)にデータをスクリプトで入れ込む際に、複数回実行されてしまいます。
起動時一回だけスクリプトを読み込むように出来ますでしょうか。
<参考>
OS…Windows 7 Professional
開発環境・・・STS3.4.0
hibernate・・・4.2.1.Final
spring-data-jpa・・・1.3.4.RELEASE
HSQLDB・・・2.2.9
【persistence.xml】
xml
1<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2<persistence xmlns="http://java.sun.com/xml/ns/persistence" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> 5 6 7 <persistence-unit name="persistenceUnit" 8 transaction-type="RESOURCE_LOCAL"> 9 <provider>org.hibernate.ejb.HibernatePersistence</provider> 10 <properties> 11 <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" /> 12 <property name="hibernate.hbm2ddl.auto" value="create-drop" /> 13 <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbc.JDBCDriver" /> 14 <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:mydata" /> 15 <property name="hibernate.hbm2ddl.import_files" value="sql/load.sql" /> 16 <property name="hibernate.show_sql" value="true"/> 17 <property name="hibernate.format_sql" value="true"/> 18 </properties> 19 </persistence-unit> 20</persistence>
【ロード用スクリプト】
load.sql
sql
1insert into T_estimate(esti_date, inq_no, ord_no, comp_flag, upd_username, reg_datetime, upd_datetime) values('2015-11-01', 1, 1, false, 'dealer', now(), now()); 2 3insert into T_estimate_details(esti_rep_no, prod_no, number, discount, upd_username, reg_datetime, upd_datetime) values(1, 'A00001',1, 1000, 'dealer', now(), now()); 4
【複数回insert文を入れようとするエンティティクラス】
※アクセサメソッドは省略
java
1package entity; 2 3import java.sql.Date; 4import java.sql.Timestamp; 5import java.util.List; 6 7import javax.persistence.CascadeType; 8import javax.persistence.Column; 9import javax.persistence.Entity; 10import javax.persistence.GeneratedValue; 11import javax.persistence.GenerationType; 12import javax.persistence.Id; 13import javax.persistence.OneToMany; 14 15@Entity 16public class T_estimate { 17 @OneToMany(cascade = CascadeType.ALL, mappedBy = "t_estimate" ) 18 @Column(nullable=true) 19 private List<T_estimate_details> t_estimate_details; 20 21 @Id 22 @GeneratedValue(strategy = GenerationType.AUTO) 23 @Column(name = "esti_rep_no") 24 private int esti_rep_no; 25 private Date esti_date; 26 private int inq_no; 27 private int ord_no; 28 private boolean comp_flag; 29 private String upd_username; 30 private Timestamp reg_datetime; 31 private Timestamp upd_datetime; 32 33 // アクセサメソッドは省略 34}
java
1package entity; 2 3import java.sql.Timestamp; 4 5import javax.persistence.Entity; 6import javax.persistence.Id; 7import javax.persistence.IdClass; 8import javax.persistence.JoinColumn; 9import javax.persistence.ManyToOne; 10 11import entity.pk.Pk_T_estimate_details; 12 13/** 14 * 見積明細テーブル 15 */ 16@Entity 17@IdClass(Pk_T_estimate_details.class) 18public class T_estimate_details { 19 @ManyToOne 20 @JoinColumn(name = "esti_rep_no", insertable = false, updatable = false) 21 private T_estimate t_estimate; 22 23 @Id 24 private int esti_rep_no; 25 @Id 26 private int esti_rep_details_no; 27 private String prod_no; 28 private int number; 29 private int discount; 30 private String upd_username; 31 private Timestamp reg_datetime; 32 private Timestamp upd_datetime; 33 34 // アクセサメソッドは省略 35}
あなたの回答
tips
プレビュー