谁能解释一下冬眠时的猫叫声吗?

时间:2022-09-07 11:08:30

I am new to hibernate and need to use one-to-many and many-to-one relations. It is a bi-directional relationship in my objects, so that I can traverse from either direction. mappedBy is the recommended way to go about it, however, I couldn't understand it. Can someone please explain to me :

我是hibernate的新手,需要使用一对多和多对一关系。它是我的对象中的一个双向关系,因此我可以从任意一个方向进行遍历。mappedBy是推荐的方法,但是,我无法理解。谁能给我解释一下:

  • what is the recommended way to use it ?
  • 推荐的使用方法是什么?
  • what purpose does it solve ?
  • 它解决了什么目的?

For the sake of my example, here are my classes with annotations :

以我的例子为例,下面是我的带有注释的类:

  • Airline OWNS many AirlineFlights
  • 航空公司拥有许多AirlineFlights
  • Many AirlineFlights belong to ONE Airline
  • 许多航空公司都属于一家航空公司

Airline:

航空公司:

@Entity 
@Table(name="Airline")
public class Airline {
    private Integer idAirline;
    private String name;

    private String code;

    private String aliasName;
    private Set<AirlineFlight> airlineFlights = new HashSet<AirlineFlight>(0);

    public Airline(){}

    public Airline(String name, String code, String aliasName, Set<AirlineFlight> flights) {
        setName(name);
        setCode(code);
        setAliasName(aliasName);
        setAirlineFlights(flights);
    }

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="IDAIRLINE", nullable=false)
    public Integer getIdAirline() {
        return idAirline;
    }

    private void setIdAirline(Integer idAirline) {
        this.idAirline = idAirline;
    }

    @Column(name="NAME", nullable=false)
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = DAOUtil.convertToDBString(name);
    }

    @Column(name="CODE", nullable=false, length=3)
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = DAOUtil.convertToDBString(code);
    }

    @Column(name="ALIAS", nullable=true)
    public String getAliasName() {
        return aliasName;
    }
    public void setAliasName(String aliasName) {
        if(aliasName != null)
            this.aliasName = DAOUtil.convertToDBString(aliasName);
    }

    @OneToMany(fetch=FetchType.LAZY, cascade = {CascadeType.ALL})
    @JoinColumn(name="IDAIRLINE")
    public Set<AirlineFlight> getAirlineFlights() {
        return airlineFlights;
    }

    public void setAirlineFlights(Set<AirlineFlight> flights) {
        this.airlineFlights = flights;
    }
}

AirlineFlights:

AirlineFlights:

@Entity
@Table(name="AirlineFlight")
public class AirlineFlight {
    private Integer idAirlineFlight;
    private Airline airline;
    private String flightNumber;

    public AirlineFlight(){}

    public AirlineFlight(Airline airline, String flightNumber) {
        setAirline(airline);
        setFlightNumber(flightNumber);
    }

    @Id
    @GeneratedValue(generator="identity")
    @GenericGenerator(name="identity", strategy="identity")
    @Column(name="IDAIRLINEFLIGHT", nullable=false)
    public Integer getIdAirlineFlight() {
        return idAirlineFlight;
    }
    private void setIdAirlineFlight(Integer idAirlineFlight) {
        this.idAirlineFlight = idAirlineFlight;
    }

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="IDAIRLINE", nullable=false)
    public Airline getAirline() {
        return airline;
    }
    public void setAirline(Airline airline) {
        this.airline = airline;
    }

    @Column(name="FLIGHTNUMBER", nullable=false)
    public String getFlightNumber() {
        return flightNumber;
    }
    public void setFlightNumber(String flightNumber) {
        this.flightNumber = DAOUtil.convertToDBString(flightNumber);
    }
}

EDIT:

编辑:

Database schema:

数据库模式:

AirlineFlights has the idAirline as ForeignKey and Airline has no idAirlineFlights. This makes, AirlineFlights as the owner/identifying entity ?

AirlineFlights有idAirline作为外国人,而航空公司没有idAirlineFlights。这使得航空公司成为所有者/识别实体?

Theoretically, I would like airline to be the owner of airlineFlights.

从理论上讲,我希望航空公司成为飞机航线的所有者。

谁能解释一下冬眠时的猫叫声吗?

5 个解决方案

#1


114  

By specifying the @JoinColumn on both models you don't have a two way relationship. You have two one way relationships, and a very confusing mapping of it at that. You're telling both models that they "own" the IDAIRLINE column. Really only one of them actually should! The 'normal' thing is to take the @JoinColumn off of the @OneToMany side entirely, and instead add mappedBy to the @OneToMany.

通过在两个模型上指定@JoinColumn,就不存在双向关系。你有两种方式的关系,这是一个非常混乱的映射。你告诉两款机型他们“拥有”IDAIRLINE专栏。真的只有一个人应该!“正常”的做法是将@JoinColumn从@OneToMany端完全去掉,而将mappedBy添加到@OneToMany。

@OneToMany(cascade = CascadeType.ALL, mappedBy="airline")
public Set<AirlineFlight> getAirlineFlights() {
    return airlineFlights;
}

That tells Hibernate "Go look over on the bean property named 'airline' on the thing I have a collection of to find the configuration."

这就告诉Hibernate“去查看一下在我有一个配置集合的东西上名为“airline”的bean属性。”

#2


222  

MappedBy signals hibernate that the key for the relationship is on the other side.

MappedBy信号休眠关系的键在另一边。

This means that although you link 2 tables together, only 1 of those tables has a foreign key constraint to the other one. MappedBy allows you to still link from the table not containing the constraint to the other table.

这意味着尽管将两个表链接在一起,但其中只有一个表对另一个表具有外键约束。MappedBy允许您仍然从不包含约束的表链接到另一个表。

#3


10  

mappedby it speak for it self, it tell hibernate don't map this field it's all ready mapped by this field [name="field"].
field is in the other entity (name of the variable in the class not the table in database)..

mappedby它为它自己说话,它告诉hibernate不要映射这个字段它已经准备好了映射到这个字段[name="field"]。字段位于另一个实体(类中变量的名称,而不是数据库中的表)。

If you don't do that hibernate will map this two relation as it's not the same relation

如果你不这么做,hibernate会映射这两个关系因为它不是相同的关系。

so we need to tell hibernate do the mapping in one side only and coordinate between them.

所以我们需要告诉hibernate只在一边做映射,并在它们之间进行协调。

#4


5  

mappedby="object of entity of same class created in another class”

mappedby=“在另一个类中创建的同一类实体的对象”

Note:-Mapped by can be used only in one class because one table must contain foreign key constraint. if mapped by can be applied on both side then it remove foreign key from both table and without foreign key there is no relation b/w two tables.

注意:- mapping by只能在一个类中使用,因为一个表必须包含外键约束。如果映射by可以在两边都应用,那么它就会从两个表中删除外键,如果没有外键,就没有关系b/w两个表。

Note:- it can be use for following annotations:- 1.@OneTone 2.@OneToMany 3.@ManyToMany

注:-可用于以下标注:- 1。@OneTone 2。@OneToMany 3 .@ManyToMany

Note---It cannot be use for following annotation :- 1.@ManyToOne

注意---它不能用于以下注释:- 1.@ManyToOne。

In one to one :- Perform at any side of mapping but perform at only one side . It will remove the extra column of foreign key constraint on the table on which class it is applied.

一对一:-在映射的任何一面执行,但只在一面执行。它将删除应用程序所在的表上的外键约束的额外列。

For eg . If we apply mapped by in Employee class on employee object then foreign key from Employee table will be removed.

如。如果我们在Employee对象上的Employee类中应用映射by,那么从Employee表中删除外键。

#5


0  

You started with ManyToOne mapping , then you put OneToMany mapping as well for BiDirectional way. Then at OneToMany side (usually your parent table/class), you have to mention "mappedBy" (mapping is done by and in child table/class), so hibernate will not create EXTRA mapping table in DB (like TableName = parent_child).

你从许多到一个映射开始,然后你把OneToMany映射也放到双向的方式上。然后在OneToMany端(通常是父表/类),必须提到“mappedBy”(映射是由子表/类完成的),因此hibernate不会在DB中创建额外的映射表(比如TableName = parent_child)。

#1


114  

By specifying the @JoinColumn on both models you don't have a two way relationship. You have two one way relationships, and a very confusing mapping of it at that. You're telling both models that they "own" the IDAIRLINE column. Really only one of them actually should! The 'normal' thing is to take the @JoinColumn off of the @OneToMany side entirely, and instead add mappedBy to the @OneToMany.

通过在两个模型上指定@JoinColumn,就不存在双向关系。你有两种方式的关系,这是一个非常混乱的映射。你告诉两款机型他们“拥有”IDAIRLINE专栏。真的只有一个人应该!“正常”的做法是将@JoinColumn从@OneToMany端完全去掉,而将mappedBy添加到@OneToMany。

@OneToMany(cascade = CascadeType.ALL, mappedBy="airline")
public Set<AirlineFlight> getAirlineFlights() {
    return airlineFlights;
}

That tells Hibernate "Go look over on the bean property named 'airline' on the thing I have a collection of to find the configuration."

这就告诉Hibernate“去查看一下在我有一个配置集合的东西上名为“airline”的bean属性。”

#2


222  

MappedBy signals hibernate that the key for the relationship is on the other side.

MappedBy信号休眠关系的键在另一边。

This means that although you link 2 tables together, only 1 of those tables has a foreign key constraint to the other one. MappedBy allows you to still link from the table not containing the constraint to the other table.

这意味着尽管将两个表链接在一起,但其中只有一个表对另一个表具有外键约束。MappedBy允许您仍然从不包含约束的表链接到另一个表。

#3


10  

mappedby it speak for it self, it tell hibernate don't map this field it's all ready mapped by this field [name="field"].
field is in the other entity (name of the variable in the class not the table in database)..

mappedby它为它自己说话,它告诉hibernate不要映射这个字段它已经准备好了映射到这个字段[name="field"]。字段位于另一个实体(类中变量的名称,而不是数据库中的表)。

If you don't do that hibernate will map this two relation as it's not the same relation

如果你不这么做,hibernate会映射这两个关系因为它不是相同的关系。

so we need to tell hibernate do the mapping in one side only and coordinate between them.

所以我们需要告诉hibernate只在一边做映射,并在它们之间进行协调。

#4


5  

mappedby="object of entity of same class created in another class”

mappedby=“在另一个类中创建的同一类实体的对象”

Note:-Mapped by can be used only in one class because one table must contain foreign key constraint. if mapped by can be applied on both side then it remove foreign key from both table and without foreign key there is no relation b/w two tables.

注意:- mapping by只能在一个类中使用,因为一个表必须包含外键约束。如果映射by可以在两边都应用,那么它就会从两个表中删除外键,如果没有外键,就没有关系b/w两个表。

Note:- it can be use for following annotations:- 1.@OneTone 2.@OneToMany 3.@ManyToMany

注:-可用于以下标注:- 1。@OneTone 2。@OneToMany 3 .@ManyToMany

Note---It cannot be use for following annotation :- 1.@ManyToOne

注意---它不能用于以下注释:- 1.@ManyToOne。

In one to one :- Perform at any side of mapping but perform at only one side . It will remove the extra column of foreign key constraint on the table on which class it is applied.

一对一:-在映射的任何一面执行,但只在一面执行。它将删除应用程序所在的表上的外键约束的额外列。

For eg . If we apply mapped by in Employee class on employee object then foreign key from Employee table will be removed.

如。如果我们在Employee对象上的Employee类中应用映射by,那么从Employee表中删除外键。

#5


0  

You started with ManyToOne mapping , then you put OneToMany mapping as well for BiDirectional way. Then at OneToMany side (usually your parent table/class), you have to mention "mappedBy" (mapping is done by and in child table/class), so hibernate will not create EXTRA mapping table in DB (like TableName = parent_child).

你从许多到一个映射开始,然后你把OneToMany映射也放到双向的方式上。然后在OneToMany端(通常是父表/类),必须提到“mappedBy”(映射是由子表/类完成的),因此hibernate不会在DB中创建额外的映射表(比如TableName = parent_child)。