在 PropertyEditor 中从数据库中获取实体

发布于 2024-10-31 01:31:42 字数 2444 浏览 0 评论 0原文

当将 PropertyEditors 与 Spring MVC 一起使用时,让它们从数据库获取实体是否不好?我应该创建一个空实体并设置其 Id 吗?

例如,对于实体 Employee:

@Entity
@Table(name = "employee")
public class Employee implements GenericEntity<Integer>{

    @Id
    @GeneratedValue
    @Column(name = "employee_id")
    public Integer getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(Integer employeeId) {
        this.employeeId = employeeId;
    }

    /** More properties here **/
}

使用以下 GenericEntityEditor 在下面的 PropertyEditor 中获取实体是不是一个坏主意:

public class GenericEntityEditor<ENTITY extends GenericEntity<Integer>> extends PropertyEditorSupport {

    private GenericDao<ENTITY, Integer> genericDao;

    public GenericEntityEditor(GenericDao<ENTITY, Integer> genericDao) {
        this.genericDao = genericDao;
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        setValue(genericDao.findById(Integer.valueOf(text)));
    }

    @SuppressWarnings("unchecked")
    @Override
    public String getAsText() {
        ENTITY entity = (ENTITY) getValue();
        if(entity == null) {
            return null;
        } 

        return String.valueOf(entity.getId());
    }
}

可以在控制器中绑定:

@Controller
public class EmployeeController {
    /** Some Service-layer resources **/

    @Resource
    private EmployeeDao employeeDao; // implements GenericDao<ENTITY, Integer> genericDao

    @SuppressWarnings("unchecked")
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Employee.class, new GenericEntityEditor(employeeDao));
    }

    /** Some request mapped methods **/

}

是否最好对 EmployeeEditor 使用更具体的方法并让它实例化一个 Employee 实体并设置其 id:

public class EmployeeEditor extends PropertyEditorSupport {

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        Employee employee = new Employee();
        employee.setId(Integer.valueOf(text));
    }

    @SuppressWarnings("unchecked")
    @Override
    public String getAsText() {
        Employee employee = (Employee) getValue();
        if(employee == null) {
            return null;
        } 

        return String.valueOf(employee.getId());
    }
}

这样,每次表单上存在 Employee 时,我们就不会往返数据库,但我不确定这是否能按 Hibernate 的预期工作?

When using PropertyEditors with Spring MVC is it bad to have them fetch entities from the database? Should I instead create an empty entity and set its Id.

For instance for the entity Employee:

@Entity
@Table(name = "employee")
public class Employee implements GenericEntity<Integer>{

    @Id
    @GeneratedValue
    @Column(name = "employee_id")
    public Integer getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(Integer employeeId) {
        this.employeeId = employeeId;
    }

    /** More properties here **/
}

Is it a bad idea to fetch the Entity in the PropertyEditor below with the following GenericEntityEditor:

public class GenericEntityEditor<ENTITY extends GenericEntity<Integer>> extends PropertyEditorSupport {

    private GenericDao<ENTITY, Integer> genericDao;

    public GenericEntityEditor(GenericDao<ENTITY, Integer> genericDao) {
        this.genericDao = genericDao;
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        setValue(genericDao.findById(Integer.valueOf(text)));
    }

    @SuppressWarnings("unchecked")
    @Override
    public String getAsText() {
        ENTITY entity = (ENTITY) getValue();
        if(entity == null) {
            return null;
        } 

        return String.valueOf(entity.getId());
    }
}

Which can be bound in the controller:

@Controller
public class EmployeeController {
    /** Some Service-layer resources **/

    @Resource
    private EmployeeDao employeeDao; // implements GenericDao<ENTITY, Integer> genericDao

    @SuppressWarnings("unchecked")
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Employee.class, new GenericEntityEditor(employeeDao));
    }

    /** Some request mapped methods **/

}

Is it preferred to use a more specific approach with a EmployeeEditor and have it just instantiate an Employee entity and set its id:

public class EmployeeEditor extends PropertyEditorSupport {

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        Employee employee = new Employee();
        employee.setId(Integer.valueOf(text));
    }

    @SuppressWarnings("unchecked")
    @Override
    public String getAsText() {
        Employee employee = (Employee) getValue();
        if(employee == null) {
            return null;
        } 

        return String.valueOf(employee.getId());
    }
}

This way we do not do a roundtrip to the DB each time an Employee exists on a Form, but I'm unsure if this works as expected with Hibernate?

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

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

发布评论

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

评论(1

み青杉依旧 2024-11-07 01:31:42

我认为这是合法的。我使用这个技术有一段时间了,效果很好。

但 Spring 3.0 有一个更好的概念。所谓的 转换器(参考章节 5.5 Spring 3 类型转换

此转换器的工作方式类似于单向属性编辑器。但它们是无状态的,因此更具性能,并且可以重用!


额外:
Spring 3.0 有一个尚未记录的功能。>3:org .springframework.core.convert.support.IdToEntityConverter

它是由 ConcersationServiceFactory 自动注册到 ConversationService 中的。

这个IdToEntityConverter会自动将所有东西(对象)转换为实体,如果实体!!有一个静态方法 find,它有一个参数,返回类型是实体的类型。

/**
 * Converts an entity identifier to a entity reference by calling a static finder method
 * on the target entity type.
 *
 * <p>For this converter to match, the finder method must be public, static, have the signature
 * <code>find[EntityName]([IdType])</code>, and return an instance of the desired entity type.
 *
 * @author Keith Donald
 * @since 3.0
 */

如果您对如何在实体中实现此类静态查找器方法有疑问。然后看一下 Spring Roo 生成的实体。

I think it is legal. I used this technice for some time, and it worked well.

But Spring 3.0 has a better concept. So called Converter (Reference Chapter 5.5 Spring 3 Type Conversion)

This converters work like one way property editors. But they are Stateless, and because of this more performat, and can be reuesed!


Added:
There is an not (yet) documented feature of Spring 3.0.>3: the org.springframework.core.convert.support.IdToEntityConverter

It is automatically registered in the ConversationService by the ConcersationServiceFactory.

This IdToEntityConverter will automatically convert everything (Object) to an Entity, if the entity!! has a static method find<entityName> which has one parameter and the return type is of type of the entity.

/**
 * Converts an entity identifier to a entity reference by calling a static finder method
 * on the target entity type.
 *
 * <p>For this converter to match, the finder method must be public, static, have the signature
 * <code>find[EntityName]([IdType])</code>, and return an instance of the desired entity type.
 *
 * @author Keith Donald
 * @since 3.0
 */

If you have doubt how to implement such a static finder method in your entity. Then have a look at Spring Roo generated entities.

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