映射实体关系和转换实体的策略 [英] Strategy for mapping entity relationships and converting entities

查看:144
本文介绍了映射实体关系和转换实体的策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习java ee 6.我的目标是使用一个简单的crud应用程序,让我创建编辑列表,并删除一个简单的id,一个名称字符串和一个描述字符串的简单实体。我遵循这一点,只是看一些代码 http://netbeans.org/kb/docs /web/jsf20-crud.html 我使用eclipse而不是netbeans和jboss 7而不是玻璃鱼。嗯....它的作品。



现在认为我的实体是用户:id,用户名,密码。
我想添加一个UserRole实体,这里是:id,rolename。
然后我设置一个OneToMany和一个ManyToOne关系。



那么,如何管理创建用户页面中的角色输入?
Netbeans crud建议的代码是一个selectonemenu(一个选择下拉列表),其中所有角色为值。在表单上,​​角色控制器(jsf managed bean)中的静态内部类roleconverter(一个facesconverter)将使用所选和提交的角色ID字符串,并使用ejb-dao查找相应的角色。



我的问题是:




  • 我明白了吗?我上面写的是否正确?

  • 正是以这种方式使用转换器最好的做法?对于这样一个简单而又复杂的任务,存在一个更好的解决方案?实体的转换器除实体名称外,都是一样的,我如何删除重复,例如:通用转换器?

  • 您认为在jsf 2中管理多对多的最佳GUI是什么? (例如具有多个角色场景的用户)


解决方案

转换器是必需的,因为HTML基本上是一个大的 String 和HTTP请求参数都是 String s。 HTML和HTTP不明白也不会绕过具体的Java对象。他们只生活在网络服务器的内存中。您真的需要能够在 SomeRandomEntity String 之间转换,以生成HTML响应并解析HTTP请求参数正确。



至于转换器不是真的 DRY ,如果所有实体都扩展了一个公共基础实体,其中 @Id 与getter和setter一起被定义,并且你有一个通用的DAO ,那么您可以为此创建一个通用转换器。这样的东西(微不足道的预先检查和 ConverterException 处理省略):

  @覆盖
public String getAsString(FacesContext context,UIComponent component,Object value){
if(value == null){
return null;
}

Long id =((BaseEntity)value).getId();
return String.valueOf(id);


@Override
@SuppressWarnings(unchecked)
public Object getAsObject(FacesContext context,UIComponent component,String value){
if( value == null){
return null;
}

Long id = Long.valueOf(value);
类< BaseEntity> type =(Class< BaseEntity>)component.getValueExpression(value)。getType(context.getELContext());
return genericDAO.find(id,type);
}

或者,在< h :selectOneMenu> ,您还可以使用基于< f:selectItem(s)> 的对象值进行转换的转换器。然而,这并不是微不足道的。 JSF2组件库 OmniFaces 具有这样一个 SelectItemsConverter 。如果有兴趣,您可能需要检查其源代码。


I am just learning java ee 6. My goal is having a simple crud application which let me create edit list and delete one trivial entity made simply by the id, a name string and a description string. I followed this just to see some code http://netbeans.org/kb/docs/web/jsf20-crud.html I used eclipse instead of netbeans and jboss 7 instead of glass fish. Well.... It works.

Now think my entity is a "User": id, username, Password. I want to add a "UserRole" entity and here it is: id, rolename. Then I set a OneToMany and a ManyToOne relationship.

So, how can I manage the role input in the create user page? Netbeans crud suggested code is a selectonemenu (a select drop down list) with all the roles ids as values. On form submit a static inner class "roleconverter" (a facesconverter) in the role controller (the jsf managed bean) takes the selected and submitted role Id string and looks up for the corresponding role using a ejb-dao.

My questions are:

  • do I understand? Is what I wrote above correct?
  • is using a converter in that way the best best practice? Doesn't a better solution exist for such a simple and recurrent task?
  • converters for entities are all the same except for the entity name, how can I remove the duplication, for example with a generic converter?
  • what's the best GUI in your opinion for managing many to many in jsf 2? (e.g. A user with multiple roles scenario)

解决方案

That's correct. The converter is required because HTML is basically one big String and HTTP request parameters are all Strings. HTML and HTTP doesn't understand nor pass around concrete Java objects. They live in webserver's memory only. You'd really need to be able to convert between SomeRandomEntity and String in order to generate HTML response and parse HTTP request parameters properly.

As to the converters being not really DRY, if all your entities extend a common base entity wherein the @Id is definied along with the getter and setter, and you've a generic DAO, then you could create a generic converter for this. Something like this (trivial prechecking and ConverterException handling omitted):

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (value == null) {
        return null;
    }

    Long id = ((BaseEntity) value).getId();
    return String.valueOf(id);
}

@Override
@SuppressWarnings("unchecked")
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    if (value == null) {
        return null;
    }

    Long id = Long.valueOf(value);
    Class<BaseEntity> type = (Class<BaseEntity>) component.getValueExpression("value").getType(context.getELContext());
    return genericDAO.find(id, type);
}

Alternatively, in the particular case of <h:selectOneMenu>, you could also use a converter which converts based on the object values of <f:selectItem(s)>. This is however not exactly trivial. The JSF2 component library OmniFaces has such a SelectItemsConverter in its assortiment. You may want to check its source code if interested.

这篇关于映射实体关系和转换实体的策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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