是否可以在java代码中使用数据库'trigger'? (hibernate + jpa + spring + mysql)

时间:2022-09-11 13:33:04

I am using mysql 5.5 DB, hibernate.4.0 as jpa provider to spring 3.0.5.

我使用mysql 5.5 DB,hibernate.4.0作为jpa提供程序来弹出3.0.5。

I have a user table and privilege table in my DB. Also I have some triggers in my DB. Those trigger implements some of our application's business logic.

我的数据库中有一个用户表和权限表。我的数据库中也有一些触发器。那些触发器实现了我们应用程序的一些业务逻辑。

For example, user john insert a record into table table_foo via my web application.

例如,用户john通过我的Web应用程序将记录插入表table_foo。

  1. The trigger BeforeInsertTableFoo will check the user's privilege. If user doesn't have the privilege, trigger will abort this operation.
  2. 触发器BeforeInsertTableFoo将检查用户的权限。如果用户没有该权限,则触发器将中止此操作。

  3. Also, if the operation is permitted, another trigger AfterInsertTableFoo will update some records in Table table_bar.
  4. 此外,如果允许该操作,则另一个触发器AfterInsertTableFoo将更新Table table_bar中的某些记录。

Is there any way that I can implement this logic as 'trigger' in java code? It seems like Jpa Event Listener and Hibernate Event can only do something like trigger but only in one table.

有没有什么办法可以在java代码中将此逻辑实现为“触发器”?似乎Jpa Event Listener和Hibernate Event只能在触发器中执行某些操作,但只能在一个表中执行。

Surely I can use a java class to do the same thing. But I can't make sure that all the other developer, even myself, will use the correct java class to do db insertion or update, when the amount of business rules increased.

当然我可以使用java类来做同样的事情。但是,当业务规则的数量增加时,我无法确保所有其他开发人员,甚至我自己都将使用正确的java类来进行数据库插入或更新。

Thx in advance. andrew

Thx提前。安德鲁

2 个解决方案

#1


2  

Check the JPA's listener more carefully. You can make one listener for many entities / tables.

更仔细地检查JPA的听众。您可以为许多实体/表创建一个侦听器。

Just set a listener class like this:

只需设置一个这样的监听器类:

public class MyListener {
@PrePersist void onPrePersist(Object entity ) {}
@PostPersist void onPostPersist(Object entity) {}
@PostLoad void onPostLoad(Object entity) {}
@PreUpdate void onPreUpdate(Object entity) {}
@PostUpdate void onPostUpdate(Object entity) {}
@PreRemove void onPreRemove(Object entity) {}
@PostRemove void onPostRemove(Object entity) {}

}

And then, for all the entities for which you want a call back or "trigger" ...

然后,对于您想要回拨或“触发”的所有实体......

@Entity @EntityListeners(MyListener.class)
   public class Customer {
    }

I hope this helps.

我希望这有帮助。

-Alex

#2


0  

I found some links on google, which talks about Spring managed event listeners with JPA

我在google上找到了一些链接,其中讨论了使用JPA的Spring管理事件监听器

#1


2  

Check the JPA's listener more carefully. You can make one listener for many entities / tables.

更仔细地检查JPA的听众。您可以为许多实体/表创建一个侦听器。

Just set a listener class like this:

只需设置一个这样的监听器类:

public class MyListener {
@PrePersist void onPrePersist(Object entity ) {}
@PostPersist void onPostPersist(Object entity) {}
@PostLoad void onPostLoad(Object entity) {}
@PreUpdate void onPreUpdate(Object entity) {}
@PostUpdate void onPostUpdate(Object entity) {}
@PreRemove void onPreRemove(Object entity) {}
@PostRemove void onPostRemove(Object entity) {}

}

And then, for all the entities for which you want a call back or "trigger" ...

然后,对于您想要回拨或“触发”的所有实体......

@Entity @EntityListeners(MyListener.class)
   public class Customer {
    }

I hope this helps.

我希望这有帮助。

-Alex

#2


0  

I found some links on google, which talks about Spring managed event listeners with JPA

我在google上找到了一些链接,其中讨论了使用JPA的Spring管理事件监听器