Hibernate更新和外键的问题

时间:2016-10-31 14:29:49

标签: java postgresql hibernate

我正在开发一个实现Hibernate的JEE开发。我过去使用过Hibernate,但从来没有设置它并进行配置。设置这个应用程序的开发人员现在已经不复存在了,那些离开的人真的不知道如何使用它(主要是)。

我们遇到的最大问题是一个模块在表上创建一个新条目。在同一进程中,后续模块想要在具有第一个表的外键的表上创建一个条目,特别是第一个模块刚刚创建的条目的键。那失败了。 Hibernate说第一把钥匙不存在。但是,如果我在测试结束后检查数据库,那么条目就在那里。

我进入调试并在尝试创建第二个条目之前停止了该过程。如果我在那时检查数据库,那么第一个条目肯定不在数据库上。自从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>

这是两个表的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;
    }
}

我们在应用程序中遇到了类似的问题。在表中创建一个条目,我们立即需要插入依赖于第一个条目的其他条目。我们一直在失败,因为它认为第一个条目不存在。

我们尝试过使用同花顺。但这并没有帮助。

我们做什么?

1 个答案:

答案 0 :(得分:0)

刚刚像这样修改了你的实体表。

@Entity
@Table(name = "zones")
@Access(AccessType.FIELD)
public class Zone extends BaseEntity implements Serializable {

    private static final long serialVersionUID = -5828384364074883963L;

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "zones", cascade =   CascadeType.ALL)
    private PartyIdTracking partyIdTracking;

    @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;
    }
    public Boolean getPartyIdTracking () {
        return this.partyIdTracking ;
    }
    public void setPartyIdTracking (PartyIdTracking partyIdTracking ) {
        this.partyIdTracking = partyIdTracking ;
    }
}

跟踪表:

@Entity
@Table(name = "party_id_tracking")
@Access(AccessType.FIELD)
public class PartyIdTracking extends BaseEntity {

    @OneToOne(fetch = FetchType.LAZY)
    private Zone zone;

    @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;
    }
    public Zone getZone() {
        return this.zone;
    }
    public void setZone(Zone zone) {
        this.zone = zone;
    }
}

希望它会有所帮助。