在PropertyEditor中从数据库中获取实体 [英] Fetching Entities from DB in a PropertyEditor
问题描述
在Spring MVC中使用PropertyEditor时,让它们从数据库中获取实体是不好的?我应该改为创建一个空实体并设置其ID。
例如,对于实体Employee:
@Entity
@Table(name =employee)
public class Employee实现GenericEntity< Integer> {
@Id
@GeneratedValue
@Column(name =employee_id)
public Integer getEmployeeId(){
return employeeId;
}
public void setEmployeeId(Integer employeeId){
this.employeeId = employeeId;
}
/ **此处有更多物业** /
}
使用以下GenericEntityEditor在PropertyEditor中获取Entity是一个坏主意:
GenericEntityEditor< ENTITY扩展GenericEntity<整数>>扩展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)));
$ b @SuppressWarnings(unchecked)
@Override
public String getAsText(){
ENTITY entity =(ENTITY)getValue();
if(entity == null){
return null;
}
return String.valueOf(entity.getId());
可以在控制器中绑定:
@Controller
公共类EmployeeController {
/ **某些服务层资源** /
@Resource
私人EmployeeDao employeeDao; //实现GenericDao< ENTITY,Integer> genericDao
@SuppressWarnings(unchecked)
@InitBinder $ b $ public void initBinder(WebDataBinder binder){
binder.registerCustomEditor(Employee.class,new GenericEntityEditor(employeeDao ));
}
/ **一些请求映射方法** /
}
是否最好对EmployeeEditor使用更具体的方法,让它只实例化一个Employee实体并设置它的ID:
public class EmployeeEditor extends PropertyEditorSupport {
$ b @Override
public void setAsText(String text)throws IllegalArgumentException {
Employee employee = new Employee( );
employee.setId(Integer.valueOf(text));
$ b @SuppressWarnings(unchecked)
@Override
public String getAsText(){
Employee employee =(Employee)getValue();
if(employee == null){
return null;
}
return String.valueOf(employee.getId());
$ / code $ / pre
这样我们就不会往返数据库每次员工存在于表单上时,但我不确定这是否按预期与Hibernate一起工作? 解决方案我认为这是合法的。我使用了这个技术一段时间,它运行良好。
但是Spring 3.0有一个更好的概念。所谓的转换器(参考章节 5.5 Spring 3类型转换)
这种转换器的工作方式与单向属性编辑器相似。但他们是无状态的,并且因为这种更多的行为,并且可以被重新引用!
增加:
There是Spring 3.0的尚未记录的功能。> 3:org.springframework .core.convert.support.IdToEntityConverter
它由ConcersationServiceFactory自动注册在ConversationService中。
IdToEntityConverter会自动将一切(对象)转换为实体,如果实体!有一个静态方法 find< entityName>
,它有一个参数,返回类型是实体的类型。
<$通过调用目标实体类型的静态查找方法
*将实体标识符转换为实体引用。
*
*< p>对于此转换器进行匹配,查找器方法必须是public,static,具有签名
*< code> find [EntityName]([IdType]) < / code> ;,并返回所需实体类型的实例。
*
* @作者Keith Donald
* @since 3.0
* /
如果您不确定如何在您的实体中实现这种静态查找方法。然后看看Spring Roo生成的实体。
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?
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.
这篇关于在PropertyEditor中从数据库中获取实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!