Contenu connexe
Similaire à JJUG CCC 2017 Spring LT about JPA (20)
Plus de Naoya Kojima (11)
JJUG CCC 2017 Spring LT about JPA
- 6. 結合してみた
--H2DB版
CREATE TABLE customer_site (
customer_site_code INT PRIMARY KEY AUTO_INCREMENT, --顧客サイトコード
url VARCHAR(1024) NOT NULL, --URL
operation_start_time TIMESTAMP NOT NULL, --運用開始時刻
operation_end_time TIMESTAMP NOT NULL, --運用終了時刻
operation_start_date TIMESTAMP NOT NULL, --運用開始日
operation_date_code INT REFERENCES operation_date(operation_date_code), --運用日コード
create_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
update_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
- 7. 結合してみた
--H2DB版
CREATE TABLE operation_date (
operation_date_code INT PRIMARY KEY AUTO_INCREMENT, --運用日コード
operation_date_mon VARCHAR(12), --運用日
operation_date_tue VARCHAR(12), --運用日
operation_date_wed VARCHAR(12), --運用日
operation_date_thu VARCHAR(12), --運用日
operation_date_fri VARCHAR(12), --運用日
operation_date_sta VARCHAR(12), --運用日
operation_date_sun VARCHAR(12), --運用日
create_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
update_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
- 8. 結合してみた
@Entity
@Table(name = "customer_site")
public class CustomerSite {
@SequenceGenerator(name = "customer_site_customer_site_code_seq",
sequenceName = "customer_site_customer_site_code_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer customerSiteCode; // 顧客サイトコード
/* 中略 */
@OneToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="operation_date_code")
private OperationDate operationDate; // 運用日エンティティ
}
- 9. 結合してみた
@Entity
@Table(name = "operation_date")
public class OperationDate {
@SequenceGenerator(name = "operation_date_operation_date_code_seq",
sequenceName = "operation_date_operation_date_code_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer operationDateCode; // 運用日コード
private String operationDateSun; // 日曜日
/* 中略 */
private String operationDateSta; // 土曜日
}
- 11. H2DBのシーケンスを使ってみた
@Entity
@Table(name = "customer_site")
public class CustomerSite {
@SequenceGenerator(name = "customer_site_customer_site_code_seq",
sequenceName = "customer_site_customer_site_code_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer customerSiteCode; // 顧客サイトコード
/* 以下略 */
}
- 12. H2DBのシーケンスを使ってみた
PostgreSQL Mode
@Entity
@Table(name = "customer_site")
public class CustomerSite {
@SequenceGenerator(name = "customer_site_customer_site_code_seq",
sequenceName = "customer_site_customer_site_code_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator = "customer_site_customer_site_code_seq")
private Integer customerSiteCode; // 顧客サイトコード
/* 以下略 */
}
このモードは @cero_tさんが教え
てくれました!
ありがとうございました!
- 13. PostgreSQLのシーケンスを使ってみた
@Entity
@Table(name = "customer_site")
public class CustomerSite {
@SequenceGenerator(name = "customer_site_customer_site_code_seq",
sequenceName = "customer_site_customer_site_code_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator = "customer_site_customer_site_code_seq")
private Integer customerSiteCode; // 顧客サイトコード
/* 以下略 */
}
H2DBからPostgresへは移行しや
すい~(^^)v
- 14. SQLServer2012 のシーケンスを使ってみた
@Entity
@Table(name = "customer_site")
public class CustomerSite {
@SequenceGenerator(name = "customer_site_customer_site_code_seq",
sequenceName = "customer_site_customer_site_code_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.AUTO,
generator = "customer_site_customer_site_code_seq")
private Integer customerSiteCode; // 顧客サイトコード
/* 以下略 */
}
SQLServerのシーケンス実装
は,2012Diarectかららしいので注意
- 15. Azure SQLServer をSpring Bootから使うとき
のappllicaiton.yml
spring:
profiles: development-sqlserver
datasource:
driver-class-name : com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://hogehoge.database.windows.net:1433;
/* 中略 */
username: hogehoge
password: fugafuga
jpa:
hibernate:
ddl-auto: none
dialect: org.hibernate.dialect.SQLServer2012Dialect
- 16. Azure SQLServer をSpring Bootから使うとき
のappllicaiton.yml
spring:
profiles: development-sqlserver
datasource:
driver-class-name : com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://hogehoge.database.windows.net:1433;
/* 中略 */
username: hogehoge
password: fugafuga
jpa:
hibernate:
ddl-auto: none
dialect: org.hibernate.dialect.SQLServer2012Dialect
余談ですが、Azure WebApps (PaaS)を使
うと、簡単にWeb Applicationをデプロイで
きるので嬉しいです