- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在基于 hibernate 的应用程序中使用hbm2ddl
来生成数据库架构。 hibernate.hbm2ddl.auto
属性的值为 create-drop
。
我正在为我的 POJO 类使用 @Entity
注释。
@Entity
public class testTable1 {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
Long id;
}
@Entity
public class testTable2 {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
Long id;
}
但是,在执行代码时,我不断获得不断递增的 Id 值。例如对于 2 个表,Id(即 Prim Key)应以 1 开头。但是在表 1
中插入记录后,序列将从 表 2
的下一个值开始。表 2 应该从 1 重新开始。我尝试了GenerationType.SEQUENCE
和GenerationType.AUTO
。没有任何效果:-(
最佳答案
当 JPA 规范未提供生成器时,您正在使用 hibernate 默认提供的全局序列生成器。要拥有私有(private)生成器,您应该使用注释 @SequenceGenerator
声明私有(private)生成器,并设置 @GenerateValue
注释的 generator
属性
摘自javadoc
@GeneratedValue
(Optional) The name of the primary key generator to use as specified in the SequenceGenerator or TableGenerator annotation.Defaults to the id generator supplied by persistence provider.
SequenceGenerator
This annotation defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation. A sequence generator may be specified on the entity class or on the primary key field or property. The scope of the generator name is global to the persistence unit (across all generator types).Example:
@SequenceGenerator(name="EMP_SEQ", sequenceName="private_sequence")
Hibernate 建议新项目使用 hibernate.id.new_generator_mappings=true,因为新的生成器更高效且更接近 JPA 2 规范语义
Section 1.3. Properties
2.2.3. Mapping identifier properties
完整示例
@Entity
@SequenceGenerator(name="PRIVATE_SEQ", sequenceName="private_sequence")
public class test {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="PRIVATE_SEQ")
Long id;
}
关于hibernate - 序列问题为什么在使用 hbm2ddl 生成架构时两个实体共享相同的序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6878093/
我使用 hibernate 作为 ORM 工具,我面临一个问题, 我需要将全局临时表映射到 HBM。现在,问题在于键,我知道在 hbm 中必须有一些关键字段,但是我应该输入什么,因为任何列在任何时间点
Hibernate 的常用配置文件主要分为 2 种:核心配置文件(hibernate.cfg.xml)和映射文件(Xxx.hbm.xml),它们主要用于配置数据库连接、事务管理、Hibernate 本
在我的 MSSQL 服务器中,我有一个名为 AllFavourite 的 SQL View 。为了将数据加载到我的 DTO 类中,我的 hbm.xml 文件中有以下内容... 在我的代码中
我有以下三个类: public class Student { private Integer studentId; private StudentSchool studentScho
我正在从 NHibernate 映射创建我的表和索引。 例如,我按如下方式创建姓氏索引: 我想创建一个复杂的索引,以便例如 LastName 和 FirstName 作为一个索引生成。这在 NH
我在映射我们的 byte[] 时遇到问题 field 。我一直在寻找几种解决方案,但到目前为止都没有奏效。我得到的只是这个异常(exception): The length of the byte[]
我正在通过代码使用 NHibernate 映射,并以这种方式创建 session 工厂: var mapper = new ModelMapper(); map
我的 hibernate hbm xml 有一个属性映射为 我正在使
我在屏幕上显示位图时遇到问题。当我编译并运行不显示任何内容时,尝试逐步调试,发现下面的代码是问题所在。 HBITMAP hbm = (HBITMAP)LoadImage(hInstance,
我是 hibernte 新手。我正在尝试使用 hibernate 从 oracle 表中简单检索记录。 我的实体类名为pojo1,pojo1.hbm.xml如下: 我在运行包含所有
例如, 如果我想要f
我正在通过代码使用 NHibernate 映射,并以这种方式创建 session 工厂: var mapper = new ModelMapper(); map
我有一个有两个主键的表,我如何在 hbm 映射文件中映射相同的表。如何定义两个主键。 最佳答案 两个主键?您可能指的是复合主键。检查this here . 关于java - 如何在 hbm 中定义
我是 hibernate 新手,遇到了一个问题。我已经阅读了 hibernate 网站上的入门指南等所有内容,但仍然无法找到解决方案。 我有一堂这样的课: public class ResultTre
我在打开 hbm 文件时遇到这个奇怪的问题。我正在使用 hibernate3.jar。我还验证了类路径中只有一个 hibernate3.jar 并且它包含 hibernate 映射 dtd 文件。 我
你能否让 1 个表被映射到 2 个不同类的 2 个不同的 HBM 引用(它们是不相关的,基本类,没有继承)。 我知道由于重复,这可能是不好的做法,但它与代码维护有关。我试图避免删除其中一个类。 谢谢,
例如, 如果我想要f
我正在开发一个 Spring 框架和 hibernate 应用程序,其中包含一个企业 Web 应用程序的中央数据库,该应用程序具有每天约有 1000 位用户在线。 您可以假设有一个计费应用程序,任何人
我是 Hibernate 新手,正在尝试插入 ArrayList在数据库中使用 .hbm 映射文件。我不想使用任何注释。我搜索了如何插入 ArrayList并从 here 找到工作代码。 我期待一个包
我在谷歌上搜索了很多,并且遵循了很多关于它的教程,但我没有让它工作。我在表 Player 和 Type 之间有多对多关系。两个表都通过 typeperplayer 连接。在表中 typeperplay
我是一名优秀的程序员,十分优秀!