简体   繁体   English

为什么在此Hibernate映射中使用@ManyToOne代替@OneToOne?

[英]Why in this Hibernate mapping it is used @ManyToOne instead @OneToOne?

I am absolutly new in Hibernate development and I have the following problem. 我对Hibernate开发绝对是陌生的,并且遇到以下问题。

I have 2 entity classes that maps 2 DB tables: 我有2个实体类,它们映射2个数据库表:

1) The first entity class (the main one) is named KM_ProjectInfo and map a DB table named KM_PROJECT . 1)第一个实体类(主要的实体类)名为KM_ProjectInfo,并映射一个名为KM_PROJECT的数据库表。

2) The second entity class is named KM_ProjectInfoStatus and map a DB table named KM_PROJECT_INFO_STATUS . 2)第二实体类名为KM_ProjectInfoStatus并映射命名KM_PROJECT_INFO_STATUS一个DB表。

So the second one represent a specific field of the first one (a status of the row representd by an instance of the KM_ProjectInfo class). 因此,第二个代表第一个的特定字段(由KM_ProjectInfo类的实例表示的行的状态)。 Infact I have something like this: 事实上,我有这样的事情:

1) KM_ProjectInfo class: 1) KM_ProjectInfo类:

@Entity
@Table(name = "KM_PROJECT")
public class KM_ProjectInfo implements Serializable {

    @Id
    @GeneratedValue
    private Long idProjectInfo;

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

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

    @ManyToOne
    @JoinColumn(name = "idCountry")
    private KMCountry country;

    @Column(name = "power")
    private long power;

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

    @ManyToOne
    @JoinColumn(name = "idProjectInfoStatus")
    private KM_ProjectInfoStatus status;

    // GETTERS & SETTERS

}

2) KM_ProjectInfoStatus : 2) KM_ProjectInfoStatus

@Entity
@Table(name = "KM_PROJECT_INFO_STATUS")
public class KM_ProjectInfoStatus implements Serializable {

    @Id
    @GeneratedValue
    private Long idProjectInfoStatus;

    @Column(name = "foldertech")
    private Long foldertech;

    @Column(name = "folderproject")
    private Long folderproject;

    // GETTERS & SETTERS

}

So, as you can see in the previous snippet, the KM_ProjectInfoStatuss is a field of the KM_ProjectInfo because I want that it contains the primary key of this table as foreign key. 所以,你可以在上面的代码片段看到,KM_ProjectInfoStatussKM_ProjectInfo的领域,因为我想,它包含此表的外键的主键。

In the logic of my application I want that at one row of the KM_PROJECT table (so at one instance of the KM_ProjectInfo entity class) is associated a single row of the KM_PROJECT_INFO_STATUS (one instance of the KM_ProjectInfoStatus entity class) because it represent a specific status for the KM_PROJECT row. 按照我的应用程序的逻辑,我希望在KM_PROJECT表的一行(因此在KM_ProjectInfo实体类的一个实例)与KM_PROJECT_INFO_STATUS单行KM_ProjectInfoStatus实体类的一个实例)相关联,因为它表示特定的状态KM_PROJECT行。

In my code I have: 在我的代码中,我有:

@ManyToOne
@JoinColumn(name = "idProjectInfoStatus")
private KM_ProjectInfoStatus status;

but I think that is wrong because at one row of my first table it is associated a specific single row of the second table. 但我认为这是错误的,因为在我的第一个表的一行中,它与第二个表的特定单行相关联。 But maybe I am missing something about how Hibernate work. 但是也许我缺少有关Hibernate如何工作的信息。

Can you help me to understand what I am missing? 您能帮我了解我的缺失吗? What it work? 它有什么用? Why I have @ManyToOne instead @OneToOne ? 为什么我有@ManyToOne而不是@OneToOne

Tnx 特纳克斯

It all depends on how you want to model things. 这完全取决于您要如何建模。 In terms of Database structure, OneToOne and ManyToOne are implemented in the same way: 就数据库结构而言, OneToOneManyToOne的实现方式相同:

  • One or more JoinColumns which makes a foreign key pointing to the primary key of the other table. 一个或多个JoinColumns ,使一个外键指向另一个表的主键。

So both solutions correctly map to your database, but it depends if you want to allow several KM_ProjectInfo to point to the same KM_ProjectInfoStatus , or only allow a single one. 因此,这两种解决方案都可以正确映射到您的数据库,但这取决于您是否要允许多个KM_ProjectInfo指向同一个KM_ProjectInfoStatus或仅允许一个。

Note that, even though you would declare a OneToOne, you could still end up with multiple KM_ProjectInfo pointing to the same KM_ProjectInfoStatus if you don't manipulate Hibernate properly. 请注意,即使您声明一个OneToOne,如果您没有正确地操作Hibernate,您仍然可能以指向同一KM_ProjectInfoStatus多个KM_ProjectInfo结尾。

Here you did not declare the reverse relationship, but if you did, the declaration would have to be different: 在这里,您没有声明反向关系,但是如果您声明了反向关系,则声明必须有所不同:

  • In case of a OneToOne , you would have a KM_ProjectInfo member 如果是OneToOne ,则您将拥有KM_ProjectInfo成员
  • In case of a OneToMany (reverse of ManyToOne ), you would have a Collection<KM_ProjectInfo> member 如果是OneToManyManyToOne反向),则您将具有Collection<KM_ProjectInfo>成员

From the description it seems you want to have one-to-one relationship. 从描述看来,您似乎想要一对一的关系。 That is the project entity should have its very own status not shared by any other project. 也就是说,项目实体应具有其自己的状态,其他任何项目都不应共享。 You could achieve this by using @OneToOne as below. 您可以通过使用@OneToOne如下实现。

@Entity
@Table(name = "KM_PROJECT")
public class KM_ProjectInfo implements Serializable {

    @Id
    @GeneratedValue
    private Long idProjectInfo;

    @OneToOne
    @JoinColumn(name = "idProjectInfoStatus")
    private KM_ProjectInfoStatus status;
}

@Entity
@Table(name = "KM_PROJECT_INFO_STATUS")
public class KM_ProjectInfoStatus implements Serializable {

    @Id
    @GeneratedValue
    private Long idProjectInfoStatus;

   @OneToOne(mappedBy="idProjectInfoStatus")
   private KM_ProjectInfo project;
}

This way you can have specific status for the KM_PROJECT. 这样,您可以具有KM_PROJECT的特定状态。

Coming back to @ManyToOne, you will want to have this if you want to share the same status with multiple projects, but that's not what you want in your case. 回到@ManyToOne,如果您想与多个项目共享相同的状态,那么您将希望拥有这个,但这并不是您要的情况。 I have tried to explain mappings in simple way here One-to-One mapping . 我试图在这里一对一映射中以简单的方式解释映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM