在hibernate中,Can实体是否在一对一映射中不存在映射?

时间:2015-01-06 00:21:43

标签: hibernate one-to-one

我是一个休眠的新家伙。假设有两个表T1和T2,其中T1的主键值也用作T2的主键。对于T2中的每一行,T1中应该有一个对应的行,但反之则不一定是真的。这是一对一的映射吗?

1 个答案:

答案 0 :(得分:1)

这将被称为使用shared primary key的一对一映射。

以下是如何将关系映射到两个实体(州和资本)的示例。

<强>表格

CREATE TABLE `capital` (
  `CAPITAL_ID` bigint(20) NOT NULL AUTO_INCREMENT,
  `NAME` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`CAPITAL_ID`),
  CONSTRAINT `STATE_ID` FOREIGN KEY (`CAPITAL_ID`) REFERENCES `state` (`STATE_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
);

CREATE TABLE `state` (
  `STATE_ID` bigint(20) NOT NULL AUTO_INCREMENT,
  `NAME` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`STATE_ID`)
);

<强>国家

@Entity
@Table(name = "STATE")
public class State {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "STATE_ID")
    private Long stateId;

    @Column(name = "NAME")
    private String name;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "state")
    private Capital capital;

    public Long getStateId() {
        return stateId;
    }

    public Capital getCapital() {
        return capital;
    }

    public void setCapital(Capital capital) {
        this.capital = capital;
    }

    public void setStateId(Long stateId) {
        this.stateId = stateId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

<强>资本

@Entity
@Table(name = "CAPITAL")
public class Capital {

    @Id
    @Column(name = "CAPITAL_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long capitalId;

    @Column(name = "NAME")
    private String name;

    @OneToOne
    @PrimaryKeyJoinColumn
    private State state;

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public Long getCapitalId() {
        return capitalId;
    }

    public void setCapitalId(Long capitalId) {
        this.capitalId = capitalId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

示例

public class HibernateApplication {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        SessionFactory factory = null;
        Session session = null;
        org.hibernate.Transaction tx = null;

        try {

            factory = HibernateUtil.getSessionFactory();
            session = factory.openSession();
            tx = session.beginTransaction();

            State state = new State();
            Capital capital = new Capital();

            state.setName("Pennsylvania");
            capital.setName("Harrisburg");
            state.setCapital(capital);
            capital.setState(state);
            session.save(state);

            tx.commit();


        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
        } finally {
            session.close();
            factory.close();
            scanner.close();
        }
    }
}