双向多对一映射

时间:2014-12-03 05:59:12

标签: java hibernate jpa mapping

我是JAVA和JPA的新手,我搜索了解决方案,但无济于事。我发现了很多例子,但它们在我的场景中没有用。 我想映射下面给出的这两个表:

书架表

CREATE TABLE "SOA2"."BOOKSHELF"  (  
"BOOKSHELF_ID" NUMBER(19,0) NOT NULL ENABLE, 
"NAME_TXT" VARCHAR2(255 CHAR), 
 PRIMARY KEY ("BOOKSHELF_ID") USING INDEX PCTFREE 10 INITRANS 2 
 MAXTRANS 255 NOCOMPRESS LOGGING
 TABLESPACE "SOA2"  ENABLE
 ) SEGMENT CREATION DEFERRED PCTFREE 10 PCTUSED 40 INITRANS 1 
 MAXTRANS 255 NOCOMPRESS LOGGING TABLESPACE "SOA2" ;

书目表

CREATE TABLE "SOA2"."BOOK" (    
"BOOK_IX" NUMBER(19,0) NOT NULL ENABLE, 
"BOOKSHELF_ID" NUMBER(19,0) NOT NULL ENABLE, 
"NAME_TXT" VARCHAR2(255 CHAR), 
 PRIMARY KEY ("BOOK_IX", "BOOKSHELF_ID") USING INDEX PCTFREE 10 INITRANS 2 
 MAXTRANS 255 NOCOMPRESS LOGGING   TABLESPACE "SOA2"  ENABLE, CONSTRAINT "FK_RJWHY4FC" 
 FOREIGN KEY ("BOOKSHELF_ID")
 REFERENCES "SOA2"."BOOKSHELF" ("BOOKSHELF_ID") ENABLE) 
 SEGMENT CREATION DEFERRED PCTFREE 10 PCTUSED 40 INITRANS 1 
  MAXTRANS 255 NOCOMPRESS LOGGING  TABLESPACE "SOA2" ;

为了便于理解它们的结构如下所示

Bookshelf Table  和 Book Table 在所有搜索之后,我使用了Eclipse“JPA Entities from Tables”工具。 它为我生成了以下类:

Bookshelf.java

@Entity
@Table(name="BOOKSHELF")
public class Bookshelf implements Serializable {
    @Id
    @Column(name="BOOKSHELF_ID", unique=true)
    private long bookshelfId;
    @Column(name="NAME_TXT")
    private String nameTxt;
    //bi-directional many-to-one association to Book
    @OneToMany(mappedBy="bookshelf", cascade={CascadeType.ALL})
    private List<Book> books;
    public List<Book> getBooks() {
        return this.books;
    }
    public void setBooks(List<Book> books) {
        this.books = books;
    }
//Code removed for the sake of brevity
}

Book.java

    @Entity
    @Table(name="BOOK")
    public class Book implements Serializable {
        @EmbeddedId
        private BookPK id;
        @Column(name="NAME_TXT", length=255)
        private String nameTxt;
        //bi-directional many-to-one association to Bookshelf
        @ManyToOne(cascade={CascadeType.ALL})
        @JoinColumn(name="BOOKSHELF_ID", nullable=false, insertable=false, updatable=false)
        private Bookshelf bookshelf;
    //Code removed for the sake of brevity
    }

BookPK.java

@Embeddable
public class BookPK implements Serializable {
@Column(name="BOOK_IX")
private long bookIx;
@Column(name="BOOKSHELF_ID", insertable=false, updatable=false, unique=true, nullable=false)
private long bookshelfId;
public BookPK(long bkShlfId, long bkId) {
    this.bookshelfId = bkShlfId;
    this.bookIx = bkId;
}
//Code removed for the sake of brevity 

}

我将其测试为: Test.java

        //Physics Bookshelf
        Bookshelf physicsShelf = new Bookshelf();
        physicsShelf.setNameTxt("Physics");
        physicsShelf.setBookshelfId(500);
        //Add Books
        Book book1 = new Book();
        book1.setNameTxt("God and the Physics");
        book1.setId(new BookPK(500, 600));

        Book book2 = new Book();
        book2.setNameTxt("Hyperspace");
        //if I pass 500 as first param in below constructor 
        //Changing 500 to 501 solves the issue
        book2.setId(new BookPK(500, 600)); //javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [books.Book#books.BookPK@8a6d]

        //Add Books into list
        List<Book> bookList = new ArrayList<Book>();
        bookList.add(book1);
        //Error: Following error appears if I uncomment the following line
        bookList.add(book2); //Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-02291: integrity constraint (SOA2.FK_RJ) violated - parent key not found
        //Set Books
        physicsShelf.setBooks(bookList);
        EntityTransaction transaction = entitymanager.getTransaction();
        transaction.begin();
        entitymanager.persist(physicsShelf);
        transaction.commit();

physicsShelf 只能成功插入一本书,如果我在 bookList 中插入第二本书,则会给出 Test.java 评论中描述的错误。< / p>

请指导我

  1. 我做错了什么以及如何纠正它。我真的被卡住了。

  2. 有没有办法通过注释其他两个类来摆脱 BookPK.java 类。

  3. 我无法更改数据库 我正在使用oracle 11g和JPA 2.1

    任何帮助都将受到高度赞赏

    此致

1 个答案:

答案 0 :(得分:1)

id应该是唯一的,但在您的情况下,您将book1和book2的id设置为Book(500,600)

book2.setId(new BookPK(500, 600)); //javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [books.Book#books.BookPK@8a6d

因此,当您尝试添加book2.setId(newBookPK(500,600));时,它会抱怨它已经存在。

尝试

book2.setId(new BookPK(500, 601));

编辑:第二个问题

我想你可以。在您的图书实体中尝试替换

@EmbeddedId
private BookPK id;

@Id
@Column(name="BOOK_IX")
private long bookIx;