CDI注入Hibernate实体 [英] CDI Injection in Hibernate Entities

查看:124
本文介绍了CDI注入Hibernate实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在我们的应用程序中使用CDI(JSR 299)(JSF2 / Seam3.0 / Hibernate 3.5.6 / GlassFish 3.1.1)



虽然我们无法我们不能在我们的Hibernate Entity类中做同样的事情。

我们有一个基类实体(@MappedSuperclass )所有实体对象派生自。

  @MappedSuperclass 
public class BaseBusinessObject实现Serializable
{
@Inject
private TestClass testClass; // FAILS
}


@Entity
@NamedQueries({@NamedQuery(name =Account.findAll,query =SELECT b FROM Account b )})
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Account扩展BaseBusinessObject
{
@Inject
private TestClass testClass; // FAILS

}

好像它可能是一个限制CDI。任何人都可以确认CDI是否适用于Hibernate实体。



任何输入值得赞赏。



谢谢,问候

解决方案

我并不真正了解CDI,但我真的不认为这是可能的。
事件如果可能的话,在很多情况下,它可能会导致一个非常糟糕的设计。



您是否希望CDI创建单个hibernate实体整个应用程序,并注入你的助手/服务/无论它在哪里?
或者你是否希望CDI使用new Entity()创建的任何实体中???b
$ b




编辑:
通常日期时间utils不包含任何状态,也不需要任何CDI注入的东西,为什么不让所有的方法都像我们在apache commons中找到的那样是静态的DateUtils?



如果您的日期时间utils需要一个状态,请将其设为单例(但注意并发问题)。

如果您的日期时间utils需要调用其他CDI bean(因此它不能是静态的),那么您宁愿将它设为单例,并在单例中注入其他CDI bean。



但这是一个坏主意。
这可能会导致拥有一个业务层来管理回调业务层的实体或类似的东西,循环依赖问题以及您的实体和业务层之间的紧密耦合。


We are using CDI(JSR 299) in our application (JSF2/Seam3.0/Hibernate 3.5.6/GlassFish 3.1.1)

While we are unable to inject resources(Helper POJOs) in our managed beans using @Inject, we cannot do the same in our Hibernate Entity classes.

We have a base entity class(@MappedSuperclass) that all entity objects derive from. CDI injection fails in both classes.

@MappedSuperclass
public class BaseBusinessObject implements Serializable
{     
    @Inject
    private TestClass testClass; //FAILS
}


@Entity
@NamedQueries({ @NamedQuery(name = "Account.findAll", query = "SELECT b FROM Account b") })
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Account extends BaseBusinessObject
{
    @Inject
    private TestClass testClass; //FAILS

}

It seems like it may be a limitation with CDI. Can anyone confirm whether CDI works with Hibernate entities.

Any inputs would be appreciated.

Thanks & Regards

解决方案

I don't really know CDI but i really don't think it is possible. Event if we could, in many cases, it would probably lead to a really bad design.

Do you wish CDI to create a single hibernate entity for the whole application, and inject your helpers/services/whatever in it? Or do you wish CDI to inject stuff in any entity you create with "new Entity()"?


Edit: Generally a Date Time utils doesn't hold any state and doesn't need any CDI injected stuff, so why not make all the methods static like what we find in apache commons DateUtils?

If your Date Time utils need a state, make it a singleton (but take care with concurrency issues).

If your Date Time utils needs to call other CDI beans (so it can't be static) then you'd rather make it a singleton, and inject on the singleton the other CDI beans.

But it's a bad idea. This may lead to have a business layer that manage entities that call back the business layer or something like that, with some circular dependency problems and a tight coupling between your entities and your business layer.

这篇关于CDI注入Hibernate实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆