English 中文(简体)
a. 使用JPA的单对一关系
原标题:one-to-one relationship using JPA

我已经利用日本邮政局制作了两个表格。 i 有必要在这些表格之间建立1-1关系。 任何人都能够告诉我如何在这些表格之间建立联系。

问题回答

简言之,在表格中添加一栏“含有”与FK制约因素的关系。 例如:

CREATE TABLE MYENTITYA (
        ID BIGINT NOT NULL,
        MYENTITYB_ID BIGINT
    );

CREATE TABLE MYENTITYB (
        ID BIGINT NOT NULL
    );

ALTER TABLE MYENTITYA ADD CONSTRAINT SQL100326144838300 PRIMARY KEY (ID);

ALTER TABLE MYENTITYB ADD CONSTRAINT SQL100326144838430 PRIMARY KEY (ID);

ALTER TABLE MYENTITYA ADD CONSTRAINT FKB65AC952578E2EA3 FOREIGN KEY (MYENTITYB_ID)
    REFERENCES MYENTITYB (ID);

这将像以下那样:

@Entity
public class MyEntityA implements Serializable {
    private Long id;
    private MyEntityB myEntityB;

    @Id
    @GeneratedValue
    public Long getId() {
        return this.id;
    }

    @OneToOne(optional = true, cascade = CascadeType.ALL)
    public MyEntityB getEntityB() {
        return this.myEntityB;
    }

    //...
}

@Entity
public class MyEntityB implements Serializable {
    private Long id;

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    //...
}

如果<代码>EntityA和之间的关系> 而后添加“条码”限制。





相关问题
query must begin with SELECT or FROM: delete [delete

I am using JPA in addition to spring(3.0.0.M4). While deleting multiple records using query.executeUpdate() i am getting the following exception. org.springframework.web.util.NestedServletException: ...

Last update timestamp with JPA

I m playing around a bit with JPA(Eclipselink to be specific). The below entity have a timestamp that s supposed to reflect whenever that entity was last updated. What are the strategies for making ...

@IdClass with non primative @Id

I m trying to add a composite primary key to a class and having a bit of trouble. Here are the classes. class User { private long id; ... } class Token { private User user; private ...

Database not dropped in between unit test

Hello good people i came accross a weird behaviour in my test.I m using JPA hibernate annotation with spring. let say i have an Class MyObject and it s property email is marqued @Column(name="EMAIL", ...

Toplink trying to persist null object

I have an object "Instance" with another object "Course" inside. When trying to persist a new Instance object, I get the following error if Course is null: java.lang.IllegalStateException: During ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...