Hbernate:单向启用级联@onetoone [英] Hbernate : unideirection @onetoone with cascade enable

查看:88
本文介绍了Hbernate:单向启用级联@onetoone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单向的Flight(Parent)和Airline(Child)之间的一对一映射,下面是我的代码 -

 公共类航空公司{
..
@OneToOne(cascade = CascadeType.ALL)
航班;
}

航班没有任何参考 Airline 这有ID,命名他们的getter和setter。



我坚持航空公司,cascade is enable 。

  Flight flight = new Flight(); 
flight.setName(flight2);

航空公司=新航空公司();
airline.setName(印度航空);
airline.setFlight(flight);

会话会话= sessionFactory.openSession();
session.saveOrUpdate(airline);
session.saveOrUpdate(flight);

这会引发异常MySQLSyntaxErrorException:'字段列表'中的未知列'flights_id'。所以我添加了 @JoinColumn 这工作得到了答案。
$ b 编辑----

但是我做了一个没有映射的双向(即两个单向关联),并没有在第二个表中应用@joinColumn,即

 公共类航空公司{
@JoinColumn(flight_ref_id)
@OneToOne(cascade = CascadeType.ALL)
航班;
}


公共类航班{
@OneToOne(cascade = CascadeType.ALL)
航空公司;



  airline.setFlight(flight); 
flight.setAirLine(airline)
Session session = sessionFactory.openSession();
session.saveOrUpdate(airline);

现在没有例外,即使没有 @joincolumn 在Flight.it中创建的列 airline_id 为什么?

  mysql>从航空公司选择* 
+ ---- + ----------- + --------------- +
| id |名称| flight_ref_id |
+ ---- + ----------- + --------------- +
| 1 |印度航空| NULL |
+ ---- + ----------- + --------------- +
1行(0.00秒)

mysql>从航班中选择*;
+ ---- + -------- + ------------ +
| id |名称| airline_id |
+ ---- + -------- + ------------ +
| 1 | Del-NJ | 1 |
+ ---- + -------- + ------------ +

并且在db flight_ref_id 中为null.why?

解决方案

您没有为数据库表指定连接列名称。假设航空公司表中有一个名为 flight_id 的连接列,您应该使用 @JoinColumn <

pre code $>公共类航空公司$ {

@OneToOne(cascade)code $ c $指定名称 -

= CascadeType.ALL)
@JoinColumn(name =flight_id)
航班;
}



编辑



您需要在关系的一侧指定 mappedBy ,如下所示 -

 公共课航班{
@OneToOne(mappedBy =航班)
航空公司;

其中 flight Airline 实体中的财产的名称。这会阻止hibernate在关系的两边生成外键列。


I have a unidirection one to one mapping between Flight(Parent) and Airline(Child),Below is my code-

public class Airline {
..
    @OneToOne(cascade=CascadeType.ALL)
    Flight flight;
}

Flight not have any reference of Airline this have id,name their getter and setter.

I am persisting airline and cascade is enable.

    Flight flight= new Flight();
    flight.setName("flight2");

    Airline airline= new Airline();
    airline.setName("Air India");
    airline.setFlight(flight);

    Session session=sessionFactory.openSession();
    session.saveOrUpdate(airline);
    session.saveOrUpdate(flight);

This throws me exception MySQLSyntaxErrorException: Unknown column 'flights_id' in 'field list'. so I added @JoinColumn This worked thanks of answers.

EDIT----

But I made a bidirectional without mappedby(ie two unidirectional associations) and didnot applied @joinColumn in second table ie

public class Airline {
        @JoinColumn("flight_ref_id")
        @OneToOne(cascade=CascadeType.ALL)
        Flight flight;
    }


public class Flight {
        @OneToOne(cascade=CascadeType.ALL)
        Airline airline;
    }

And

    airline.setFlight(flight);
    flight.setAirLine(airline)
    Session session=sessionFactory.openSession();
    session.saveOrUpdate(airline);

Now there is no exception,even no @joincolumn in Flight.it created column airline_id why?

mysql> select * from Airline;
+----+-----------+---------------+
| id | name      | flight_ref_id |
+----+-----------+---------------+
|  1 | Air India |          NULL |
+----+-----------+---------------+
1 row in set (0.00 sec)

mysql> select * from Flight;
+----+--------+------------+
| id | name   | airline_id |
+----+--------+------------+
|  1 | Del-NJ |          1 |
+----+--------+------------+

And in db flight_ref_id is null.why?

解决方案

You are not specifying the join column name for your db table. Assuming the airline table has a join column named flight_id, you should use @JoinColumn to specify the name -

public class Airline {

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "flight_id")
    Flight flight;
}

EDIT

You need to specify mappedBy in one side of the relation, like below -

public class Flight {
    @OneToOne(mappedBy = "flight")
    Airline airline;
}

where flight is the name of the property in the Airline entity. This will prevent hibernate to generate foreign key columns on both sides of the relation.

这篇关于Hbernate:单向启用级联@onetoone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆