これはなに?

テーブル作成

er.png
-- Table: mother_tbl
-- Table: child_tbl

ALTER TABLE child_tbl DROP CONSTRAINT child_tbl_motherid_fkey;
DROP TABLE IF EXISTS child_tbl;
DROP TABLE IF EXISTS mother_tbl;

CREATE TABLE mother_tbl
(
  id bigserial NOT NULL,
  version timestamp,
  name character varying(255),
  CONSTRAINT id_mother PRIMARY KEY (id)
);

CREATE TABLE child_tbl
(
  id bigserial NOT NULL,
  version timestamp without time zone,
  name character varying(255),
  motherid bigint,
  CONSTRAINT id_child PRIMARY KEY (id),
  CONSTRAINT child_tbl_motherid_fkey FOREIGN KEY (motherid)
      REFERENCES mother_tbl (id) MATCH SIMPLE
      ON UPDATE CASCADE
      ON DELETE CASCADE
);


ALTER TABLE mother_tbl
  OWNER TO postgres;

ALTER TABLE child_tbl
  OWNER TO postgres;

INSERT INTO mother_tbl (version, name) VALUES (current_timestamp, 'm1');
INSERT INTO mother_tbl (version, name) VALUES (current_timestamp, 'm2');

INSERT INTO child_tbl (version, name, motherid) 
VALUES (current_timestamp, 'c1', (SELECT id FROM mother_tbl WHERE name = 'm1'));

INSERT INTO child_tbl (version, name, motherid) 
VALUES (current_timestamp, 'c2', (SELECT id FROM mother_tbl WHERE name = 'm1'));

INSERT INTO child_tbl (version, name, motherid) 
VALUES (current_timestamp, 'c3', (SELECT id FROM mother_tbl WHERE name = 'm1'));

INSERT INTO child_tbl (version, name, motherid) 
VALUES (current_timestamp, 'c4', (SELECT id FROM mother_tbl WHERE name = 'm2'));

INSERT INTO child_tbl (version, name, motherid) 
VALUES (current_timestamp, 'c5', (SELECT id FROM mother_tbl WHERE name = 'm2'));

INSERT INTO child_tbl (version, name, motherid) 
VALUES (current_timestamp, 'c6', (SELECT id FROM mother_tbl WHERE name = 'm2'));

Entity 作成

EntityManager?#remove(obj) でカスケード削除

public class App {

    public static void main(String[] args) {
        EntityManagerFactory emf 
                = Persistence.createEntityManagerFactory("com.mycompany_JPAExam_jar_1.0-SNAPSHOTPU");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        
        TypedQuery<MotherTbl> query 
                = em.createNamedQuery("MotherTbl.findByName", MotherTbl.class);
        query.setParameter("name", "m1");
        MotherTbl mother = query.getSingleResult();        
        em.remove(mother);
        
        tx.commit();
    }
}

JPQL の DELETE 文でカスケード削除

public class App {

    public static void main(String[] args) {
        EntityManagerFactory emf 
                = Persistence.createEntityManagerFactory("com.mycompany_JPAExam_jar_1.0-SNAPSHOTPU");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        
        Query query = em.createQuery("DELETE FROM MotherTbl m WHERE m.name = :name");
        query.setParameter("name", "m1");
        int updateRows = query.executeUpdate();
        
        System.out.println(updateRows);
        
        tx.commit();
    }
}

子から親への FK に ON DELETE CASCADE を指定していない場合、親への DELET 文を発行したらどうなるか?

子から親への FK に ON DELETE CASCADE をつけてんだから em.remove(m) で、child_tbl への delete 文を発行しないで欲しい


Java#Glassfish


添付ファイル: fileres2.png 2233件 [詳細] fileres1.png 2290件 [詳細] fileer.png 2515件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2013-10-28 (月) 00:49:13 (4055d)
Short-URL: http://at-sushi.com/pukiwiki/index.php?cmd=s&k=c8c905c145
ISBN10
ISBN13
9784061426061