@GeneratedValue(strategy = GenerationType.IDENTITY)不适用于@EmbeddedId [英] @GeneratedValue(strategy = GenerationType.IDENTITY) does not work with @EmbeddedId

查看:1283
本文介绍了@GeneratedValue(strategy = GenerationType.IDENTITY)不适用于@EmbeddedId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Hibernate的新手,需要一些帮助。



以前我只有1个主键,我使用 @GeneratedValue(strategy = GenerationType.IDENTITY)来创建我的数据库中有一个序列。



现在,我已经更改为使用 @EmbeddedId Employee.java



由于某些原因,增量排序不再有效。对于我所有的新条目,它只是0。



对此的一些澄清将非常感激。我怎样才能自动增加值?



谢谢!

Employee.java - 新

  @Entity 
@Table(name =employee)
public类Employee实现Serializable {

@EmbeddedId
私有EmployeePK pk;
私人字符串名称;
私人字符串用户名;
$ b public Employee(){
}

public Employee(String username,String name){
super();
this.pk =新的EmployeePK(用户名);
this.name = name;
}

@Embeddable
public class EmployeePK implements Serializable {

@GeneratedValue(strategy = GenerationType.IDENTITY)
private int employeeId;
私人字符串用户名;
$ b $ public EmployeePK(){
}
$ b $ public EmployeePK(String username){
this.username = username;
}

public int getEmployeeId(){
return employeeId;
}

public void setEmployeeId(int employeeId){
this.employeeId = employeeId;
}

public String getUsername(){
return username;
}

public void setUsername(String username){
this.username = username;
}

@Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + getOuterType()。hashCode();
result = prime * result + employeeId;
result = prime * result +((username == null)?0:username.hashCode());
返回结果;

$ b @Override
public boolean equals(Object obj){
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass()!= obj.getClass())
return false;
EmployeePK other =(EmployeePK)obj;
if(!getOuterType()。equals(other.getOuterType()))
return false;
if(employeeId!= other.employeeId)
return false;
if(username == null){
if(other.username!= null)
return false;
} else if(!username.equals(other.username))
return false;
返回true;
}

私人雇员getOuterType(){
return Employee.this;
}

}

public int getEmployeeId(){
return pk.getEmployeeId();


public String getUsername(){
return pk.getUsername();
}

public String getName(){
return name;
}

public void setName(String name){
this.name = name;
}
}


解决方案

< a href =http://docs.oracle.com/javaee/7/api/javax/persistence/GeneratedValue.html =nofollow> @GeneratedValue JavaDoc提到:

lockquote
GeneratedValue注解可以应用于实体或映射超类的主键属性或字段结合Id注释。使用GeneratedValue注释只需要支持简单主键。 GeneratedValue注释的使用不支持衍生主键






然而,Hibernate可能根据他们的文档支持这样的构造( 2.2.3.2.4。部分标识符生成)。还有一个示例代码,所以请随时尝试一下。这部分文档还包含以下警告:


Hibernate团队一直认为这样的构造从根本上是错误的。在使用此功能之前,请尽量修复您的数据模型。


所以也许更好的解决方案是重新考虑模型比试图让它起作用(如果可以的话)。


I am new to Hibernate and need some help here.

Previously I had only 1 primary key and I used the @GeneratedValue(strategy = GenerationType.IDENTITY) to create a sequence in my database.

Now, I have changed to composite primary keys using @EmbeddedId and below my new Employee.java.

For some reason, the increment sequencing no longer works. It just stays at 0 for all my new entries.

Some clarification of this would be most appreciated. And how can I go about automatically incrementing the value?

Thank you!

Employee.java - NEW

@Entity
@Table(name = "employee")
public class Employee implements Serializable {

    @EmbeddedId
    private EmployeePK pk;
    private String name;
    private String username;

    public Employee() {
    }

    public Employee(String username, String name) {
        super();
        this.pk = new EmployeePK(username);
        this.name = name;
    }

    @Embeddable
    public class EmployeePK implements Serializable {

        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int employeeId;
        private String username;

        public EmployeePK() {
        }

        public EmployeePK(String username) {
            this.username = username;
        }

        public int getEmployeeId() {
            return employeeId;
        }

        public void setEmployeeId(int employeeId) {
            this.employeeId = employeeId;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + getOuterType().hashCode();
            result = prime * result + employeeId;
            result = prime * result + ((username == null) ? 0 : username.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            EmployeePK other = (EmployeePK) obj;
            if (!getOuterType().equals(other.getOuterType()))
                return false;
            if (employeeId != other.employeeId)
                return false;
            if (username == null) {
                if (other.username != null)
                    return false;
            } else if (!username.equals(other.username))
                return false;
            return true;
        }

        private Employee getOuterType() {
            return Employee.this;
        }

    }

    public int getEmployeeId() {
        return pk.getEmployeeId();
    }

    public String getUsername() {
        return pk.getUsername();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

解决方案

@GeneratedValue JavaDoc mentions:

The GeneratedValue annotation may be applied to a primary key property or field of an entity or mapped superclass in conjunction with the Id annotation. The use of the GeneratedValue annotation is only required to be supported for simple primary keys. Use of the GeneratedValue annotation is not supported for derived primary keys.


However Hibernate probably supports such constructs according to their documenation (2.2.3.2.4. Partial identifier generation). There is also an example code, so feel free to give it a try. This section of the documentation also contains following warning:

The Hibernate team has always felt such a construct as fundamentally wrong. Try hard to fix your data model before using this feature.

So maybe better solution would be to rethink the model rather than to try to get this to work (if you can).

这篇关于@GeneratedValue(strategy = GenerationType.IDENTITY)不适用于@EmbeddedId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆