3

あるデータベースから別のデータベースにすべてのデータを挿入しようとしています。これら 2 つのデータベースは同じ構造を持っています (同じエンティティを使用します)。私は PostgreSQL 9.1、Glassfish 4.0、EclipseLink (JPA 2.1)、および Java EE 7 Web を使用しています。

これらはエンティティです:

@Entity
public class Store extends BaseEntity {

    @NotNull
    private String name;

   /*
    * required
    */
    @OneToMany(cascade = CascadeType.PERSIST)
    private List<Price> listPrices;

   /*
    * required
    */
    @OneToMany(cascade = CascadeType.PERSIST)
    private List<BusinessHours> listBusinessHours;

    @OneToOne(cascade = CascadeType.PERSIST, optional = false)
    private PointCoordinates pointCoordinates;
    ...
}

たとえば、@OneToMany アノテーション付きエンティティの 1 つ:

@Entity
public class BusinessHours extends BaseEntity {

    private Boolean holiday;

    ...
}

BaseEntity には、GenerationType.SEQUENCE と serialVersionUID を持つ Id が含まれています。

persistence.xml:

<persistence-unit name="restDBFromRawData" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/RestDBFromRawData</jta-data-source>

    <class>info.mycompany.entities.BusinessHours</class>
    <class>info.mycompany.entities.Store</class>
    <class>info.mycompany.entities.PointCoordinates</class>
    <class>info.mycompany.entities.Price</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
    </properties>
  </persistence-unit>

<persistence-unit name="restClientDB" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/RestClientDB</jta-data-source>

    <class>info.mycompany.entities.BusinessHours</class>
    <class>info.mycompany.entities.Store</class>
    <class>info.mycompany.entities.PointCoordinates</class>
    <class>info.mycompany.entities.Price</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
      <property name="eclipselink.ddl-generation.output-mode" value="both"/>
    </properties>
   </persistence-unit>

1 つのデータベースからすべてのデータを取得して、他のデータベースに挿入しようとすると (addStoresToRestClientDB):

@Stateless
public class StoreOutputService {

    @EJB
    StoreOutputDAO storeOutputDAO;

    @EJB
    StoreOutputRestClientDAO storeOutputRCDAO;

    public List<Store> getAllStores() {
        return storeOutputDAO.findAll();
    }

    public void addStoresToRestDBFromRawData() {
        //this works fine
        ...
        for (Store store : listStore) {
            storeOutputDAO.edit(store);
        }
    }

    public void addStoresToRestClientDB() {

        List<Store> listStore = getAllStores();
        //size of list is right, the same like I insert in "addStoresToRestDBFromRawData"

        for (Store store : listStore) {
            storeOutputRCDAO.edit(store);
        }
    }
}

次のエラーが表示されます。

WARNING:   Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: insert or update on table "store_businesshours" violates foreign key constraint "fk_store_businesshours_listbusinesshours_id"
  Detail: Key (listbusinesshours_id)=(55) is not present in table "businesshours".
Error Code: 0
Call: INSERT INTO STORE_BUSINESSHOURS (listBusinessHours_ID, Store_ID) VALUES (?, ?)
    bind => [2 parameters bound]
Query: DataModifyQuery(name="listBusinessHours" sql="INSERT INTO STORE_BUSINESSHOURS (listBusinessHours_ID, Store_ID) VALUES (?, ?)")
...
Caused by: org.postgresql.util.PSQLException: ERROR: insert or update on table "store_businesshours" violates foreign key constraint "fk_store_businesshours_listbusinesshours_id"
  Detail: Key (listbusinesshours_id)=(55) is not present in table "businesshours".
...
WARNING:   StandardWrapperValve[info.mycompany.web.ApplicationConfig]: Servlet.service() for servlet info.mycompany.web.ApplicationConfig threw exception
org.postgresql.util.PSQLException: ERROR: insert or update on table "store_businesshours" violates foreign key constraint "fk_store_businesshours_listbusinesshours_id"
  Detail: Key (listbusinesshours_id)=(55) is not present in table "businesshours".

DAO は問題ないはずです。StoreOutputDAOPersistence Unit を使用し、 Persistence UnitrestDBFromRawDataStoreOutputRestClientDAO使用しrestClientDBます。

JDBCjdbc/RestDBFromRawDataは ressourcetype (クラス名: org.postgresql.ds.PGConnectionPoolDataSource) を持つ接続プールを使用しjavax.sql.ConnectionPoolDataSource、他の JDBC "jdbc/RestClientDB" は "javax.sql.XADataSource" ressourcetype (クラス名: org.postgresql.xa.PGXADataSource) を使用します。「jdbc/RestDBFromRawData」のように「jdbc/RestClientDB」で同じ設定を行うと、次のエラー メッセージが表示されます。「ローカル トランザクションにはすでに 1 つの非 XA リソースがあります」

$GLASSFISH_HOME/glassfish/lib$GLASSFISH_HOME/domains/domain1/libPostgreSQL ドライバー「postgresql-9.1-901.jdbc4.jar .

データベースでpgAdminを調べてみると、すべて問題ないようです。それらは同じ構造を持ち、他のすべてが正常に機能します。別のデータベースにデータを挿入し、StoreOutputDAOexcept を使用してデータを挿入しますStoreOutputRestClientDAO

BaseEntity を更新します。

@MappedSuperclass
public abstract class BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}
4

1 に答える 1

2

このエラーは、営業時間が他のデータベースに存在しないために発生します。必ず挿入してください。

その ID はどのように定義されていますか? 他のデータベースに移行するときに、ID とバージョンを null にする必要がある場合があります。

于 2013-09-03T13:59:25.643 に答える