使用序列和全局序列

时间:2017-07-10 12:00:11

标签: java hibernate spring-mvc

package com.abc.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

    @Entity (name = "Employee")
    @Table(name="EMPLOYEE")
    public class EmployeeEntity {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, 
    generator="oracle_seq")
    @SequenceGenerator(name="oracle_seq", 
    sequenceName="MY_ENTITY_SEQ")
    private Integer id;

    @Column(name="FIRSTNAME")
    private String firstname;

    @Column(name="LASTNAME")
    private String lastname;

    @Column(name="EMAIL")
    private String email;

    @Column(name="TELEPHONE")
    private String telephone;


    public String getEmail() {
        return email;
    }
    public String getTelephone() {
        return telephone;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
    public String getFirstname() {
        return firstname;
    }
    public String getLastname() {
        return lastname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
}

重要提示:如果我们没有指定自己的序列,@ GeneneValue(strategy = GenerationType.AUTO)将使用单个全局序列hibernate_sequence

但是获取不能将null插入(" DB"," EMPLOYEE"," ID") 另一种创建序列的方法

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="oracle_seq")
@SequenceGenerator(name="oracle_seq", sequenceName="MY_ENTITY_SEQ")

但这也是给予的 org.springframework.beans.factory.BeanCreationException:创建名称为' editEmployeeController'的注册自动连接依赖项失败; {{some nested exception}} 未知ID。 generator:ORACLE_SEQ

以下是来自数据库浏览器的图片

Here I have created sequence in db click here see image

Hibernate配置文件

<hibernate-configuration>
    <session-factory>
        <mapping class="com.howtodoinjava.entity.EmployeeEntity" />
    </session-factory>
</hibernate-configuration>

0 个答案:

没有答案