对同一类中的多个字段使用

对同一类中的多个字段使用

本文介绍了对同一类中的多个字段使用@Id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Hibernate的新手,必须支持使用Hibernate的现有应用程序
这是一个纯粹的报表应用程序 - 无需插入/更新/删除 - 只选择

我可以看到现有的POJO对象在同一个类中的多个字段中使用了@Id批注。我的理解是,对于复合主键 - 你需要使用@Embeddable和@EmbeddedId



然而,这并没有在我的类中定义,奇怪的是代码编译和运行很好



以下是我的代码示例:

  package com.xyz.vo; 

import java.io.Serializable;
import javax.persistence。*;
import org.hibernate.annotations.GenericGenerator;

@Entity(name =com.xyz.vo.Emp)
@Table(name =TEmployee)
public class Emp实现Serializable {
private java.lang.Integer empId;
@Id
@Column(name =EMP_ID)
public java.lang.Integer getEmpId(){
return this.empId;
}

public void setEmpId(java.lang.Integer empId){
this.empId = empId;
}

private java.lang.Integer empAge;
@Id
@Column(name =EMP_AGE)
public java.lang.Integer getEmpAge(){
return this.empAge;
}

public void setEmpAge(java.lang.Integer empAge){
this.empAge = empAge;
}

private String empName;
@Column(name =EMP_NAME)
public String getEmpName(){
return this.empName;
}

public void setEmpName(String empName){
this.empName = empName;
}

以及pojo中的更多字段 - 没有定义@Id注意 - 这里有两个字段有@Id注释

我很困惑这个编译和工作的原因是什么?
Thanks
akila

解决方案

根据Hibernate文档,Hibernate似乎允许这样做,但它不是但是符合JPA。



对于符合JPA的解决方案,您必须指定一个额外的@IDClass这个设置(或使用EmbeddedId):


I am a newbie to Hibernate and have to support an existing application that uses Hibernate It is a pure reporting application - no insert / update / delete - only selects

I can see existing POJO objects which have the @Id annotation used in more than one field in the same class

My understanding was that for a composite primary key - you need to use @Embeddable and @EmbeddedId

However this is not defined in my class and the strange thing is code compiles and runs just fine

Here is an example of my code :

package com.xyz.vo;

import java.io.Serializable;
import javax.persistence.*;
import org.hibernate.annotations.GenericGenerator;

@Entity(name="com.xyz.vo.Emp")
@Table(name = "TEmployee")
public class Emp implements Serializable    {
private java.lang.Integer  empId;
@Id
@Column(name = "EMP_ID")
public java.lang.Integer  getEmpId(){
    return this.empId;
}

public  void  setEmpId(java.lang.Integer empId){
    this.empId=empId;
}

private java.lang.Integer  empAge;
@Id
@Column(name = "EMP_AGE")
public java.lang.Integer  getEmpAge(){
    return this.empAge;
}

    public  void  setEmpAge(java.lang.Integer empAge){
    this.empAge=empAge;
}

    private String empName;
    @Column(name = "EMP_NAME")
public String  getEmpName(){
    return this.empName;
}

    public  void  setEmpName(String empName){
    this.empName=empName;
}

and many more fields in the pojo - which do not have the @Id defined

NOTE - here two fields have the @Id annotation

I am confused why this compiles and works ?Thanksakila

解决方案

According to the Hibernate docs, Hibernate seems to allow this but it is not however JPA compliant.

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#d0e4819

For a JPA compliant solution you would have to specify an additional @IDClass for this set-up (or use an EmbeddedId):

http://www.objectdb.com/java/jpa/entity/id

这篇关于对同一类中的多个字段使用@Id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:11