休眠和数据库触发器

编程入门 行业动态 更新时间:2024-10-26 05:27:05
本文介绍了休眠和数据库触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在数据库中使用了hibernate和预插入触发器。让我解释一下这个场景。 我有两个表格,分别是A和B.在这两个表格中,主键通过预插入触发器插入。 A中的主键是B中的外键。因此,当我插入这些表中时,B中的外键列应该填充触发值A(主键值)。但它并没有像我期待的那样发生。两个表中的主键都被正确插入,但外键列保持取值0而不是实际得到的触发值。 表有一对多的关系。

这两个表就像 -

class Employee { private int RECORDID; @OneToMany(cascade = cascadeType.ALL) @JoinColumn(name =MASTERRECORDID,referencedColumnName =RECORDID) private Collection< EmployeeDetails> employeeDetails = new ArrayList< EmployeeDetails>(); } class EmployeeDetails { private int RECORDID; private int MASTERRECORDID;

谢谢解决方案

Hibernate不支持由开箱即用的触发器生成的主键。

您可以使用在插入触发器和ID生成器之前:

class Employee { @Id @GeneratedValue generator =trigger_gen) @GenericGenerator(name =trigger_gen, value =jpl.hibernate.util.TriggerAssignedIdentityGenerator) private int RECORDID; ... }

或者,如果您的实体拥有非PK您可以使用内置选择生成器。

另请参阅:

  • 5.1.2.2。标识符生成器

I am using hibernate and pre-insert triggers in database. Let me explain the scenario. I have two tables say A and B. In both the tables Primary keys are inserted through pre-insert triggers. Primary key from A is foreign key in B. So when I insert into these tables the foreign key column in B should get populated with triggered value of A (the primary key value). But its not happening as I am expecting. Primary keys in both tables are inserted properly but the foreign key column keeps getting value 0 instead of the triggered value which it should actually get. Table have one-to-many relationship.

The two tables are like -

class Employee { private int RECORDID; @OneToMany(cascade=cascadeType.ALL) @JoinColumn(name="MASTERRECORDID" , referencedColumnName="RECORDID") private Collection<EmployeeDetails> employeeDetails = new ArrayList<EmployeeDetails>(); } class EmployeeDetails{ private int RECORDID; private int MASTERRECORDID; }

Thanks

解决方案

Hibernate doesn't support primary keys generated by triggers out of the box.

You can use a custom identity generator described in Before Insert Trigger and ID generator:

class Employee { @Id @GeneratedValue(generator = "trigger_gen") @GenericGenerator(name = "trigger_gen", value = "jpl.hibernate.util.TriggerAssignedIdentityGenerator") private int RECORDID; ... }

Alternatively, if your entities have non-PK unique field, you can use built-in select generator.

See also:

  • 5.1.2.2. Identifier generator

更多推荐

休眠和数据库触发器

本文发布于:2023-10-26 11:49:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530044.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:触发器   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!