java 如何在类中映射没有属性 id 的休眠映射文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17106100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:00:49  来源:igfitidea点击:

How to map hibernate mapping file without attribute id in the class?

javahibernate

提问by lvarayut

I have created a simple class and a simple tables of database to test the hibernate concepts. The class is named "Employee":

我创建了一个简单的类和一个简单的数据库表来测试休眠概念。该类名为“员工”:

public class Employee {
   private String firstName; 
   private String lastName;   
   private int salary;  
   public Employee(String fname, String lname, int salary) {
      this.firstName = fname;
      this.lastName = lname;
      this.salary = salary;
   }
   ...
}

The database names "EMPLOYEE":

数据库名称为“EMPLOYEE”:

create table EMPLOYEE (
   id INT NOT NULL auto_increment,
   first_name VARCHAR(20) default NULL,
   last_name  VARCHAR(20) default NULL,
   salary     INT  default NULL,
   PRIMARY KEY (id)
);

This is my Employee.hbm.xml:

这是我的 Employee.hbm.xml:

<hibernate-mapping>
   <class name="Employee" table="EMPLOYEE">
      <id name="id" type="int" column="id">
         <generator class="native"/>
      </id>
      ...
      ...
   </class>
</hibernate-mapping>

So, you can notice that in my class it doesn't have the "id" attribute. However, I create the id column in my database to let it be the primary key which is automatically generated. In this case, I don't know what should I put in the <id name="?" type="int" column="id">to map my class with my database. If I ignore it, would it be some problems afterward?

因此,您会注意到在我的课程中它没有“id”属性。但是,我在我的数据库中创建了 id 列,让它成为自动生成的主键。在这种情况下,我不知道我应该放什么<id name="?" type="int" column="id">来映射我的类和我的数据库。如果我忽略它,以后会不会出现一些问题?

采纳答案by Anantha Sharma

I can understand where you are coming from, but Hibernate identifies Objects (in first and 2nd level cache) using the ID of the object, so an object without an ID cannot be represented in hibernate.

我可以理解您来自哪里,但是 Hibernate 使用对象的 ID 识别对象(在一级和二级缓存中),因此没有 ID 的对象无法在 hibernate 中表示。

I would recommend you to create the field with generator class as nativeand let the db deal with the value.

我建议您使用生成器类创建字段,native并让数据库处理该值。

so in annotation you'd say

所以在注释中你会说

@Id @GeneratedValue(strategy=GenerationType.NATIVE)
private Integer id;

if you don't want to repeat this entry multiple times you can create a base class (base pojo) which has this and extend the class in all your models.

如果你不想多次重复这个条目,你可以创建一个基类(base pojo),它有这个并在你的所有模型中扩展这个类。

回答by user3604017

You can use Composite Key Concept , if you don't want to use @Id, You can use @EmbededId annotation.

您可以使用 Composite Key Concept ,如果您不想使用 @Id,则可以使用 @EmbededId 批注。

回答by blackpanther

In my experience, you should keep the id as an attribute of the POJO (Employee class). The reason for this is because the id may be needed when you want to search for a particular Employee object in the database (Hibernate Session's get()and load()methods require the id). In addition, because you have mapped the id as an attribute in the Hibernate Mapping file, it should therefore correspond with the POJO (Employee class) definition of properties/attributes.

根据我的经验,您应该将 id 保留为 POJO(Employee 类)的一个属性。这样做的原因是因为当您想在数据库中搜索特定的 Employee 对象时可能需要 id(Hibernate Sessionget()load()方法需要 id)。此外,由于您已将 id 映射为 Hibernate 映射文件中的一个属性,因此它应该与属性/属性的 POJO(员工类)定义相对应。