编程语言
首页 > 编程语言> > java-Hibernate不使用Firebird序列,而是尝试使用表

java-Hibernate不使用Firebird序列,而是尝试使用表

作者:互联网

我正在尝试从休眠状态将新用户插入Firebird数据库

Session session = factory.openSession();

UserDetail user = new UserDetail();
user.setName("Mark");
user.setPassword("1234567");
user.setUserType(1L);

session.beginTransaction();
try {
    session.persist(user);
    session.getTransaction().commit();
} catch (Exception  e) {
    session.getTransaction().rollback();
} finally {
    session.close();
}

而我的实体是

@Entity
@Table(name = "USER_DETAIL")
public class UserDetail
{
    @Id
    @Column(name = "ID", nullable = false)
    @SequenceGenerator(name = "gen", sequenceName = "GEN_USER_DETAIL_ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "gen")
    private Long id;

    @Column(name = "TYPE_ID")
    @NotNull
    private Long userType;

    @Column(name = "NAME")
    @NotNull
    @Size(min = UserDetailConstraint.MIN_USER_NAME, max = UserDetailConstraint.MAX_USER_NAME)
    private String name;

    @Column(name = "HASHED_PASSWORD")
    @NotNull
    @Size(min = UserDetailConstraint.MIN_PASSWORD, max = UserDetailConstraint.MAX_PASSWORD)
    private String password;

    public Long getId() { return id;  }
    public void setId(Long id) { this.id = id; }

    public Long getUserType() { return userType; }
    public void setUserType(Long userType) { this.userType = userType; }

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


    public String getPassword() { return password; }
    public void setPassword(String password) { this.password = password; }
}

但是当休眠尝试通过生成器检索新的ID时,它将使用

select next_val as id_val from GEN_USER_DETAIL_ID with lock

但是GEN_USER_DETAIL_ID不是表,而是序列.

因此程序因错误而崩溃

WARN: HHH10001002: Using Hibernate built-in connection pool (not for 

production use!)
апр 26, 2017 5:19:06 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.firebirdsql.jdbc.FBDriver] at URL [jdbc:firebirdsql://localhost:3050/warehouse]
апр 26, 2017 5:19:06 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=SYSDBA, password=****}
апр 26, 2017 5:19:06 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
апр 26, 2017 5:19:06 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 5 (min=1)
апр 26, 2017 5:19:06 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.FirebirdDialect
апр 26, 2017 5:19:06 PM org.hibernate.id.enhanced.SequenceStyleGenerator configure
INFO: HHH000107: Forcing table use for sequence-style generator due to pooled optimizer selection where db does not support pooled sequences
апр 26, 2017 5:19:07 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 4.3.2.Final
Hibernate: select next_val as id_val from GEN_USER_DETAIL_ID with lock
апр 26, 2017 5:19:07 PM org.hibernate.id.enhanced.TableStructure$1$1 execute
ERROR: could not read a hi value
org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544569. Dynamic SQL Error
SQL error code = -204
Table unknown
GEN_USER_DETAIL_ID
At line 1, column 51

当我使用xml映射而不是注释时,则一切正常.

我怎样才能解决这个问题?

解决方法:

我找到了一种使用火鸟发电机的方法.需要这个.

@Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "gen")
    @GenericGenerator(name = "gen", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
            parameters = {@org.hibernate.annotations.Parameter(name = "sequence_name", value = "GEN_USER_DETAIL_ID")
            })

标签:hibernate,firebird,java
来源: https://codeday.me/bug/20191111/2019880.html