从PropertyEditor中的DB中获取实体

Emi*_*l H 5 java spring hibernate spring-mvc

在Spring MVC中使用PropertyEditors时,让它们从数据库中获取实体是不是很糟糕?我应该创建一个空实体并设置其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 **/
}
Run Code Online (Sandbox Code Playgroud)

使用以下GenericEntityEditor获取下面PropertyEditor中的Entity是一个坏主意:

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

哪个可以绑定在控制器中:

@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 **/

}
Run Code Online (Sandbox Code Playgroud)

是否优先使用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());
    }
}
Run Code Online (Sandbox Code Playgroud)

这样,每当一个Employee存在于Form上时,我们都不会对DB进行往返,但我不确定这是否与Hibernate一样正常工作?

Ral*_*lph 6

我认为这是合法的.我使用这种技术已经有一段时间了,它运作良好.

但Spring 3.0有一个更好的概念.所谓的转换器(参考第5.5Spring 3类型转换)

这种转换器就像单向属性编辑器一样工作.但他们是无国籍的,因为这种更好的形式,可以被重新考虑!


补充:Spring 3.0 还有一个尚未记录的功能.> 3:org.springframework.core.convert.support.IdToEntityConverter

它由ConcersationServiceFactory自动在ConversationService中注册.

如果实体!!这个IdToEntityConverter将自动将所有(Object)转换为实体!有一个静态方法find<entityName>,它有一个参数,返回类型是实体的类型.

/**
 * 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
 */
Run Code Online (Sandbox Code Playgroud)

如果您怀疑如何在您的实体中实现这样的静态查找程序方法.然后看看Spring Roo生成的实体.