0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【質問】[Spring data][JPA]JPAで結合したデータが取れません

Last updated at Posted at 2015-11-24

現在、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 version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">


	<persistence-unit name="persistenceUnit"
		transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<properties>
			<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
			<property name="hibernate.hbm2ddl.auto" value="create-drop" />
			<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbc.JDBCDriver" />
			<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:mydata" />
			<property name="hibernate.hbm2ddl.import_files" value="sql/load.sql" />
			<property name="hibernate.show_sql" value="true"/>
			<property name="hibernate.format_sql" value="true"/>
		</properties>
	</persistence-unit>
</persistence>

【ロード用スクリプト】
load.sql

insert 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());

insert 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());

【複数回insert文を入れようとするエンティティクラス】
※アクセサメソッドは省略

package entity;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class T_estimate {
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "t_estimate" )
	@Column(nullable=true)
	private List<T_estimate_details> t_estimate_details;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "esti_rep_no")
	private int esti_rep_no;
	private Date esti_date;
	private int inq_no;
	private int ord_no;
	private boolean comp_flag;
	private String upd_username;
	private Timestamp reg_datetime;
	private Timestamp upd_datetime;

        // アクセサメソッドは省略
}
package entity;

import java.sql.Timestamp;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import entity.pk.Pk_T_estimate_details;

/**
 * 見積明細テーブル
 */
@Entity
@IdClass(Pk_T_estimate_details.class)
public class T_estimate_details {
	@ManyToOne
	@JoinColumn(name = "esti_rep_no", insertable = false, updatable = false)
	private T_estimate t_estimate;

	@Id
	private int esti_rep_no;
	@Id
	private int esti_rep_details_no;
	private String prod_no;
	private int number;
	private int discount;
	private String upd_username;
	private Timestamp reg_datetime;
	private Timestamp upd_datetime;

        // アクセサメソッドは省略
}
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?