私はHibernateを実装しているJEE開発に取り組んでいます。私は過去にHibernateを使用しましたが、セットアップして設定する必要はありませんでした。このアプリケーションを設定している開発者は、いまやなくなり、残っている開発者は、実際にそれを使用する方法(ほとんど)を知ることができません。Hibernateのアップデートと外部キーに関する問題
私たちが直面している最大の問題は、1つのモジュールがテーブルに新しいエントリを作成することです。同じプロセスで、後続のモジュールは、最初のテーブルへの外部キー、特に最初に作成したエントリのキーを持つテーブルにエントリを作成します。それは失敗する。 Hibernateによると、最初のキーは存在しません。しかし、テストがデータベースの上にあることを確認したら、そのエントリはそこにあります。
私はデバッグを開始し、2番目のエントリを作成しようとする直前にプロセスを停止しました。その時点でデータベースをチェックすると、最初のエントリはまだデータベースにはありません。私はHibernateが更新をキャッシュして以来、それに驚いていませんでした。それはそれ自身のスケジュールを物理的に更新するだけです。しかし、私が理解できないのは、最初のエントリーが存在しないと言っている理由です。これを引き起こしているのは何ですか?
私はセットアップを示すために何を与えるべきかわからないので、いくつかのことを提供し、うまくいけばフィードバックを得ることができます。
Wildfly 10、Hibernate 5.1.0.Final、Postgresql 9.4.1208.jre7を使用しています。ここで
がWildFlyのstandalone.xmlでのデータソースの設定です<subsystem xmlns="urn:jboss:domain:datasources:4.0">
<datasources>
<xa-datasource jndi-name="java:jboss/datasources/CMA_DS" pool-name="java:jboss/datasources/CMA_DS" enabled="true">
<xa-datasource-property name="DatabaseName">
cma
</xa-datasource-property>
<xa-datasource-property name="ServerName">
localhost
</xa-datasource-property>
<xa-datasource-property name="PortNumber">
5432
</xa-datasource-property>
<xa-datasource-property name="User">
user
</xa-datasource-property>
<xa-datasource-property name="Password">
psw
</xa-datasource-property>
<xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
<driver>postgres</driver>
<xa-pool>
<min-pool-size>20</min-pool-size>
<max-pool-size>50</max-pool-size>
<prefill>true</prefill>
</xa-pool>
</xa-datasource>
<drivers>
<driver name="postgres" module="org.postgresql">
<xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>
のpersistence.xmlは含まれてい
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="CMA_DS">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/CMA_DS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="com.cma.config.FixedPostgreSQL94Dialect" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.cache.default_cache_concurrency_strategy" value="nonstrict-read-write" />
<property name="hibernate.max_fetch_depth" value="1" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.generate_statistics" value="false" />
<property name="hibernate.c3p0.min_size" value="15" />
<property name="hibernate.c3p0.max_size" value="50" />
<property name="hibernate.c3p0.timeout" value="300" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="3000" />
</properties>
</persistence-unit>
そして、この2つのテーブルのためのSQLです:
CREATE TABLE zones
(
id numeric NOT NULL, -- Generated id field
description character varying(250),
lat_degree numeric,
lat_minute numeric,
lat_hemisphere character varying(1),
long_degree numeric,
long_minute numeric,
long_hemisphere character varying(1),
national_loc_id character varying(61),
global_loc_num character varying(13),
zone_name character varying(50) NOT NULL,
country_code character(3),
party_organization_id character varying(15),
owner_nation character(3),
local_owner boolean,
CONSTRAINT pk_zones PRIMARY KEY (id),
CONSTRAINT zones_global_loc_num_key UNIQUE (global_loc_num),
CONSTRAINT "zones_national_loc-id_key" UNIQUE (national_loc_id, country_code),
CONSTRAINT zones_zone_name_key UNIQUE (zone_name, owner_nation)
);
CREATE TABLE party_id_tracking
(
id numeric NOT NULL,
zone_id numeric,
endpoint_id numeric,
notified_date timestamp without time zone NOT NULL DEFAULT now(),
CONSTRAINT pk_party_id_tracking PRIMARY KEY (id),
CONSTRAINT party_id_tracking_endpoint_fk FOREIGN KEY (endpoint_id)
REFERENCES routing_endpoint (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT party_id_tracking_zone_fk FOREIGN KEY (zone_id)
REFERENCES zones (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
);
ゾーンテーブルのエンティティ:
@Entity
@Table(name = "zones")
@Access(AccessType.FIELD)
public class Zone extends BaseEntity implements Serializable {
private static final long serialVersionUID = -5828384364074883963L;
@Column(name = "zone_name")
private String zoneName;
@Column(name = "description")
private String description;
@Column(name = "lat_degree")
private Integer latDegree;
@Column(name = "lat_minute")
private Float latMinute;
@Column(name = "lat_hemisphere")
private String latHemisphere;
@Column(name = "long_degree")
private Integer longDegree;
@Column(name = "long_minute")
private Float longMinute;
@Column(name = "long_hemisphere")
private String longHemisphere;
@Column(name = "national_loc_id")
private String nationalLocID;
@Column(name = "global_loc_num")
private String globalLocNum;
@Column(name = "country_code")
private String countryCode;
@Column(name = "owner_nation")
private String ownerNation;
@Column(name = "party_organization_id")
private String partyOrganizationId;
@Column(name = "local_owner")
private Boolean localOwner;
public Zone() {
super();
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getLatDegree() {
return this.latDegree;
}
public void setLatDegree(Integer latDegree) {
this.latDegree = latDegree;
}
public String getLatHemisphere() {
return this.latHemisphere;
}
public void setLatHemisphere(String latHemisphere) {
this.latHemisphere = latHemisphere;
}
public Integer getLongDegree() {
return this.longDegree;
}
public void setLongDegree(Integer longDegree) {
this.longDegree = longDegree;
}
public String getLongHemisphere() {
return this.longHemisphere;
}
public void setLongHemisphere(String longHemisphere) {
this.longHemisphere = longHemisphere;
}
public String getNationalLocID() {
return this.nationalLocID;
}
public void setNationalLocID(String nationalLocID) {
this.nationalLocID = nationalLocID;
}
public String getGlobalLocNum() {
return this.globalLocNum;
}
public void setGlobalLocNum(String globalLocNum) {
this.globalLocNum = globalLocNum;
}
public Float getLatMinute() {
return this.latMinute;
}
public void setLatMinute(Float latMinute) {
this.latMinute = latMinute;
}
public Float getLongMinute() {
return this.longMinute;
}
public void setLongMinute(Float longMinute) {
this.longMinute = longMinute;
}
public String getZoneName() {
return this.zoneName;
}
public void setZoneName(String zoneName) {
this.zoneName = zoneName;
}
public String getCountryCode() {
return this.countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getOwnerNation() {
return this.ownerNation;
}
public void setOwnerNation(String ownerNation) {
this.ownerNation = ownerNation;
}
public String getPartyOrganizationId() {
return this.partyOrganizationId;
}
public void setPartyOrganizationId(String partyOrganizationId) {
this.partyOrganizationId = partyOrganizationId;
}
public Boolean getLocalOwner() {
return this.localOwner;
}
public void setLocalOwner(Boolean localOwner) {
this.localOwner = localOwner;
}
}
とトラッキングテーブル:
@Entity
@Table(name = "party_id_tracking")
@Access(AccessType.FIELD)
public class PartyIdTracking extends BaseEntity {
@Column(name = "notified_date")
@Temporal(TemporalType.TIMESTAMP)
private Date notifiedDate;
@Column(name = "endpoint_id")
private Long endpointId;
@Column(name = "zone_id")
private Long zoneId;
public Date getNotifiedDate() {
return this.notifiedDate;
}
public void setNotifiedDate(Date notifiedDate) {
this.notifiedDate = notifiedDate;
}
public Long getEndpointId() {
return this.endpointId;
}
public void setEndpointId(Long endpointId) {
this.endpointId = endpointId;
}
public Long getZoneId() {
return this.zoneId;
}
public void setZoneId(Long zoneId) {
this.zoneId = zoneId;
}
}
我々はアプリケーション内の複数のトランザクションで同様の問題を抱えています。エントリがテーブルに作成され、すぐにその最初のエントリに依存する他のエントリを挿入する必要があります。我々は最初のエントリが存在しないと考えて失敗を続ける。
私たちはflushを使ってみました。しかし、それは役に立たなかった。
私たちは何をしますか?
エンティティからのマッピングはどこからですか? party_id_trackingからゾーンへの外部キーがあり、適切にマップする必要があります。私はそれが助けると思う。 – Killer