Hibernate 忽略了我的持久调用?

发布于 2024-12-06 12:31:03 字数 1241 浏览 6 评论 0原文

我有一个奇怪的问题。无论我做什么,我都无法通过休眠将新记录放入数据库中。我将 Hibernate 与 Tapestry 和 MySQL 一起使用。请有人帮助我!

我有 UserDAO 类和这段代码:

@CommitAfter
public boolean saveUser(User user){
    try {
        session.persist(user);
        return true;
    }catch(Exception e){
        return false;
    }
}

然后我在这里调用它:

@OnEvent(component="add")
Object onAdd(){
    if(username!=null && password!=null){
        User user = new UserBean();
        user.setUsername(username);
        user.setPassword(password);
        userService.saveUser(user);
    }
    if(eventName!=null){
        Event event = new EventBean();
        event.setName(eventName);
        eventService.saveEvent(event);
    }
    return this;
}

但它不起作用,我不知道为什么,请帮忙!

这是完整的项目: http://www.mediafire.com/?pqb2aaadhbukuav


我添加了这段AppModule.java 中的代码现在可以工作了

@Match("*DAO")
public static void adviseTransactions(HibernateTransactionAdvisor advisor, MethodAdviceReceiver receiver) {
    advisor.addTransactionCommitAdvice(receiver);
}

有人可以向我解释这段代码在做什么吗? 这不是我第一次使用 hibernate 和 Tapestry,我以前从未见过这个,所以我不明白?请任何人

i have one weird problem. No matter what i do, i cant put new record in database via hibernate. I'm using Hibernate with Tapestry and MySQL. Please someone help me!

I have UserDAO class with this piece of code:

@CommitAfter
public boolean saveUser(User user){
    try {
        session.persist(user);
        return true;
    }catch(Exception e){
        return false;
    }
}

And then i call it here:

@OnEvent(component="add")
Object onAdd(){
    if(username!=null && password!=null){
        User user = new UserBean();
        user.setUsername(username);
        user.setPassword(password);
        userService.saveUser(user);
    }
    if(eventName!=null){
        Event event = new EventBean();
        event.setName(eventName);
        eventService.saveEvent(event);
    }
    return this;
}

But its not working, i dont know why, please help!

Here is full project: http://www.mediafire.com/?pqb2aaadhbukuav


I added this piece of code in AppModule.java and now it works

@Match("*DAO")
public static void adviseTransactions(HibernateTransactionAdvisor advisor, MethodAdviceReceiver receiver) {
    advisor.addTransactionCommitAdvice(receiver);
}

Can anyone explain to me what is this code doing?
This is not my first time working with hibernate and tapestry, and i never saw this before, so i don't understand? Please anyone

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风不识月 2024-12-13 12:31:03

默认情况下,@CommitAfter 注释仅适用于页面/组件类。要在服务对象中获得相同的行为,您需要额外的代码段。 用户指南中的此页面的后半部分对此进行了介绍。

谁能向我解释一下这段代码在做什么

该代码在名称与 @Match("*DAO") 匹配的任何服务中查找 @CommitAfter 注释。然后,它应用 HibernateTransactionAdvisor,如果带注释的方法成功退出,它会添加一个 commit() 调用。这是使用 Tapestry 的一些类似 AOP 的元编程 功能来完成的。

The @CommitAfter annotation only works in page/component classes by default. To get the same behaviour in service objects, you need that extra piece of code. This is covered by the second half of this page from the user guide.

Can anyone explain to me what is this code doing

That code looks for @CommitAfter annotations in any services having a name that matches @Match("*DAO"). It then applies the HibernateTransactionAdvisor, which adds a commit() call if the annotated method exits successfully. This is done using some of Tapestry's AOP-like meta-programming features.

爱你不解释 2024-12-13 12:31:03

您可以在 saveUser 方法中记录异常吗?如果持久性中出现问题,您将不会知道,因为您忽略了异常。如果抛出异常,它可能有助于查找问题。

另一个问题可能是事务管理 - 如果您直接使用 hibernate,则需要在事务中调用 persist 方法。如果没有它,更改可能会被忽略。

Can you log the exception in the saveUser method - if something is going wrong in the persist you won't know about it because you are ignoring the exception. If an exception is being thrown it might help find the problem.

The other problem could be transaction management - if you are using hibernate directly you will need to call the persist method inside a transaction. Without it the changes might be ignored.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文