简体   繁体   中英

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

I am absolutly new in Hibernate development and I have the following problem.

I have 2 entity classes that maps 2 DB tables:

1) The first entity class (the main one) is named KM_ProjectInfo and map a DB table named KM_PROJECT .

2) The second entity class is named KM_ProjectInfoStatus and map a DB table named KM_PROJECT_INFO_STATUS .

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). Infact I have something like this:

1) KM_ProjectInfo class:

@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 :

@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.

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.

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.

Can you help me to understand what I am missing? What it work? Why I have @ManyToOne instead @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:

  • One or more JoinColumns which makes a foreign key pointing to the primary key of the other table.

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.

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.

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
  • In case of a OneToMany (reverse of ManyToOne ), you would have a Collection<KM_ProjectInfo> member

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.

@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.

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. I have tried to explain mappings in simple way here One-to-One mapping .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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