代码之家  ›  专栏  ›  技术社区  ›  M-Razavi

Hibernate中的双向一对一关系

  •  0
  • M-Razavi  · 技术社区  · 7 年前

    发动机表由以下部分组成 [Engine_ID, Engine_Name,Airplane_Owner_ID] .
    [Airplane_ID, Left_Engine, Right_Engine]

    Left\u Engine和Right\u Engine是引擎表中的外键,此外,Planet\u Owner\u ID是Planet表中的外键。因此,在飞机和发动机表之间定义了三个一对一的关系。

    我知道如何指定两个表之间的单个一对一关系,但如何指定两个表之间的多个关系?这是相同的过程吗?

    如何在Hibernate中指定这些关系?

    2 回复  |  直到 6 年前
        1
  •  2
  •   APC    7 年前

    “Left_Engine和Right_Engine是引擎表中的外键,
    此外,Planet\u Owner\u ID是飞机表中的外键。”

    Airplane 被引用 Engine 发动机 飞机 。在数据模型中,每个表都是另一个表的子表。循环依赖关系在数据库中和在堆栈的其他部分中一样糟糕。

    • Left_Engine Right_Engine 飞机
    • 添加 Engine_Position 发动机
    • 在上添加唯一约束 Engine (Airplane_Owner_ID, Engine_Position)
    • 发动机位置

    该模型有两个优点:

    1. 明确所有权-飞机拥有引擎,引擎不拥有飞机。
        2
  •  0
  •   M-Razavi    7 年前

    飞机.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-mapping">
    <hibernate-mapping>
        <class name="Airplane" table="AIRPLANE">
            <id name="id" type="int" column="AIRPLANE_ID">
                <generator class="native"/>
            </id>
            <property name="name" column="AIRPLANE_NAME" type="string" length="250"/>
            <many-to-one name="rightEngine" class="Engine" cascade="save-update" unique="true"/>
            <many-to-one name="leftEngine" class="Engine" cascade="save-update" unique="true"/>
        </class>
    </hibernate-mapping>
    

    Engine.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-mapping">
    <hibernate-mapping>
        <class name="Engine" table="ENGINE">
            <id name="id" type="int" column="ENGINE_ID">
                <generator class="native"/>
            </id>
            <property name="name" column="ENGINE_NAME" type="string" length="250"/>
            <property name="position" column="ENGINE_POSITION" type="java.lang.Byte" />
    
            <one-to-one name="ownerAirplane" property-ref="rightEngine" />
        </class>
    </hibernate-mapping>
    

    public class Airplane {
        private int id;
        private String name;
    
        private Engine rightEngine;
        private Engine leftEngine;
    
        public Airplane(String name) {
            this.name = name;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Engine getRightEngine() {
            return rightEngine;
        }
    
        public void setRightEngine(Engine rightEngine) {
            this.rightEngine = rightEngine;
        }
    
        public Engine getLeftEngine() {
            return leftEngine;
        }
    
        public void setLeftEngine(Engine leftEngine) {
            this.leftEngine = leftEngine;
        }
    
        @Override
        public String toString() {
            return "Airplane{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", rightEngline=" + (rightEngine == null ? null : rightEngine.getName()) +
                    ", leftEngine=" + (leftEngine == null ? null : leftEngine.getName()) +
                    '}';
        }
    }
    

    public class Engine {
    
        private int id;
        private String name;
        private byte position;//0=left, 1=right
        private Airplane ownerAirplane;
    
        /**
         * @param name
         * @param position 0=left, 1=right
         */
        public Engine(String name, byte position) {
            this.name = name;
            this.position = position;
        }
    
        public Airplane getOwnerAirplane() {
            return ownerAirplane;
        }
    
        public void setOwnerAirplane(Airplane ownerAirplane) {
            this.ownerAirplane = ownerAirplane;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
    
        public void setName(String name) {
            this.name = name;
        }
    
        /**
         * @return 0=left, 1=right
         */
        public byte getPosition() {
            return position;
        }
    
        /**
         * @param position 0=left, 1=right
         */
        public void setPosition(byte position) {
            this.position = position;
        }
    
    
        @Override
        public String toString() {
            return "Engine{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", position=" + position +
                    ", ownerAirplane=" + (ownerAirplane == null ? null : ownerAirplane.getName()) +
                    '}';
        }
    }
    

    主类

     public static void main(final String[] args) throws Exception {
    
            Session session = ourSessionFactory.openSession();
            Transaction transaction = null;
            try {
                transaction = session.beginTransaction();
                Engine engineRight1 = new Engine("engineRight1", (byte) 1);
                Engine engineLeft1 = new Engine("engineLeft1", (byte) 0);
                Airplane airplane1 = new Airplane("Airplane1");
    
                Engine engineRight2 = new Engine("engineRight2", (byte) 1);
                Engine engineLeft2 = new Engine("engineLeft2", (byte) 0);
                Airplane airplane2 = new Airplane("Airplane2");
    
                Engine engineRight3 = new Engine("engineRight3", (byte) 1);
                Engine engineLeft3 = new Engine("engineLeft3", (byte) 0);
                Airplane airplane3 = new Airplane("Airplane3");
    
                engineLeft1.setOwnerAirplane(airplane1);
                engineRight1.setOwnerAirplane(airplane1);
                airplane1.setLeftEngine(engineLeft1);
                airplane1.setRightEngine(engineRight1);
    
                engineRight2.setOwnerAirplane(airplane2);
                airplane2.setRightEngine(engineRight2);
    //            airplane2.setLeftEngine(engineLeft1);
    
                engineRight3.setOwnerAirplane(airplane3);
                airplane3.setLeftEngine(engineLeft3);
    
                session.save(airplane1);
                session.save(airplane2);
                session.save(airplane3);
    
                session.save(engineLeft1);
                session.save(engineLeft2);
                session.save(engineLeft3);
                session.save(engineRight1);
                session.save(engineRight2);
                session.save(engineRight3);
    
                transaction.commit();
            } catch (HibernateException e) {
                transaction.rollback();
                e.printStackTrace();
            }