Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate @GeneratedValue(策略=GenerationType.IDENTITY)不适用于@EmbeddedId_Hibernate_Identity - Fatal编程技术网

Hibernate @GeneratedValue(策略=GenerationType.IDENTITY)不适用于@EmbeddedId

Hibernate @GeneratedValue(策略=GenerationType.IDENTITY)不适用于@EmbeddedId,hibernate,identity,Hibernate,Identity,我刚开始冬眠,需要一些帮助 以前我只有一个主键,我使用@GeneratedValue(strategy=GenerationType.IDENTITY)在数据库中创建序列 现在,我已经使用@EmbeddedId和我的新Employee.java更改为复合主键 出于某种原因,增量排序不再有效。对于我的所有新条目,它只停留在0 如能对此作出澄清,将不胜感激。我怎样才能自动递增这个值呢 谢谢大家! Employee.java-新建 @Entity @Table(name = "employee") p

我刚开始冬眠,需要一些帮助

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

现在,我已经使用
@EmbeddedId
和我的新
Employee.java
更改为复合主键

出于某种原因,增量排序不再有效。对于我的所有新条目,它只停留在0

如能对此作出澄清,将不胜感激。我怎样才能自动递增这个值呢

谢谢大家!

Employee.java-新建

@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;
    }
}
JavaDoc提到:

GeneratedValue注释可与Id注释一起应用于实体或映射超类的主键属性或字段。仅要求简单主键支持使用GeneratedValue注释。派生主键不支持使用GeneratedValue注释


然而,Hibernate根据其文档()可能支持这样的构造。还有一个示例代码,请随意尝试一下。本节文档还包含以下警告:

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


因此,也许更好的解决方案是重新考虑模型,而不是尝试让它正常工作(如果可以)。

我认为您使用的是Postgres,而不是在DB中将数据类型从int更改为serial