Fetching Entities from DB in a PropertyEditor

后端 未结 1 964
说谎
说谎 2021-02-10 06:25

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

相关标签:
1条回答
  • 2021-02-10 06:51

    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.

    0 讨论(0)
提交回复
热议问题