2012-09-02 35 views
1

我已經搜查幾乎無處不在,但似乎沒有人有這個問題。 我有Spring MVC應用程序。 有作爲請求範圍在上下文配置文件豆被聲明,並通過引用UsersController實體UserImpl。 UsersController引用的UserImpl對象實際上是一個CGLib代理(here是爲什麼?)。現在,當我第一次嘗試這樣的堅持代理對象使用HibernateTemplate我得到這個錯誤:應堅持CGLIB代理實體使用HibernateTemplate

org.hibernate.MappingException: Unknown entity: main.mvc.model.hibernate.UserImpl$$EnhancerByCGLIB$$9ac49631

經過一番搜索,我發現這樣的:

org.springframework.orm.hibernate3.support.ScopedBeanInterceptor

這還挺解決了這個問題。現在我沒有得到MappingException並且實體被持久化了,但是......數據庫記錄中的每個字段都是空的。有什麼問題?

注: 當我硬編碼UserImpl實例,並用UserServiceImpl堅持一切是OK。情境配置的

部分:

<bean id="userService" class="main.mvc.model.hibernate.UserServiceImpl"/> 
<bean id="userValidator" class="main.validators.UserValidator"/> 
<bean id="userWrapperValidator" class="main.validators.UserWrapperValidator"/> 
<bean id="user" class="main.mvc.model.hibernate.UserImpl" scope="request"> 
    <aop:scoped-proxy/> 
</bean> 
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="packagesToScan" value="main.mvc.model.hibernate"/> 
    <property name="entityInterceptor"> 
<bean class="org.springframework.orm.hibernate3.support.ScopedBeanInterceptor"/> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.hbm2ddl.auto">update</prop> 
     </props> 
    </property> 
</bean> 

UsersController:

package main.mvc.controllers; 

import java.util.List; 

import javax.inject.Inject; 
import javax.validation.Valid; 

import main.helpers.UserWrapper; 
import main.mvc.model.User; 
import main.mvc.model.UserService; 
import main.mvc.model.hibernate.UserImpl; 
import main.validators.UserValidator; 
import main.validators.UserWrapperValidator; 

import org.apache.log4j.Logger; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.WebDataBinder; 
import org.springframework.web.bind.annotation.InitBinder; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.mvc.support.RedirectAttributes; 


@Controller 
@RequestMapping("/users") 
public class UsersController { 

    private User user; // should be request-scoped 
    private UserService userService; 
    private UserValidator userValidator; 
    private UserWrapperValidator userWrapperValidator; 

    @SuppressWarnings("unused") 
    private Logger logger = Logger.getLogger(getClass()); 

    @Inject 
    public UsersController(User user, UserService userService, UserValidator userValidator, UserWrapperValidator userWrapperValidator){ 
     this.user = user; 
     this.userService = userService; 
     this.userValidator = userValidator; 
     this.userWrapperValidator = userWrapperValidator; 
    } 

    @InitBinder("user") 
    protected void setUserValidator(WebDataBinder binder){ 
     binder.setValidator(userValidator); 
    } 

    @InitBinder("userWrapper") 
    protected void setUserWrapperValidator(WebDataBinder binder){ 
     binder.setValidator(userWrapperValidator); 
    } 

    @ModelAttribute 
    public UserWrapper wrapUser(){ 
     return new UserWrapper(user); 
    } 

    //==================================REQUEST HANDLERS========================================== 

    @RequestMapping(value={"/new"}, method=RequestMethod.GET) 
    public String showCreationForm(@ModelAttribute UserWrapper userWrapper){ 
     return "user_registration_form"; 
    } 

    @RequestMapping(method=RequestMethod.POST) 
    public String createUser(@Valid @ModelAttribute UserWrapper userWrapper, BindingResult bindingResult, RedirectAttributes redirectAttributes){ 
     if(bindingResult.hasErrors()){ 
      return "user_registration_form"; 
     } 
     userService.save(userWrapper.getUser()); 
     redirectAttributes.addFlashAttribute("message_title", "Jakiś text"); 
     redirectAttributes.addFlashAttribute("message_text", "Jakiś text"); 
     redirectAttributes.addFlashAttribute("redirect_url", "/users"); 
     return "redirect:information"; 
    } 
... 
} 

UserServiceImpl:

public class UserServiceImpl extends HibernateDaoSupport implements UserService { 

    private PasswordEncoder passwordEncoder; 
    private SaltSource saltSource; 

    @Inject 
    public UserServiceImpl(SessionFactory sessionFactory, PasswordEncoder passwordEncoder, SaltSource saltSource) { 
     super(); 
     setSessionFactory(sessionFactory); 
     setPasswordEncoder(passwordEncoder); 
     setSaltSource(saltSource); 
    } 

    public void save(User user) { 
     Date creationTime = new Date(); 
     user.setCreationDate(creationTime); 
     user.setLastModified(creationTime); 

     List<Role> roles = new LinkedList<>(); 
     roles.add(Role.ROLE_USER); 
     user.setRoles(roles); 

     Logger.getLogger(getClass()).info(user); 
     getHibernateTemplate().save(user); 
    } 
... 
} 

更新
好了,所以我增加了一些AOP魔法加輸出什麼在ScoppedBeanInterceptor中處理實體對象時使用筆。它完好無損。雖然Hibernate的BasicBinder將空值綁定到SQL語句。任何幫助?

日誌:

11:27:51,866 DEBUG org.hibernate.SQL:111 - select userimpl0_.id as id1_, userimpl0_.creationDate as creation2_1_, userimpl0_.lastModified as lastModi3_1_, userimpl0_.city as city1_, userimpl0_.country as country1_, userimpl0_.latitude as latitude1_, userimpl0_.longitude as longitude1_, userimpl0_.postalCode as postalCode1_, userimpl0_.street as street1_, userimpl0_.birthDate as birthDate1_, userimpl0_.firstName as firstName1_, userimpl0_.secondName as secondName1_, userimpl0_.sex as sex1_, userimpl0_.surName as surName1_, userimpl0_.alias as alias1_, userimpl0_.enabled as enabled1_, userimpl0_.loginEmail as loginEmail1_, userimpl0_.password as password1_, userimpl0_.preferences_id as prefere19_1_ from TBL_USER userimpl0_ where userimpl0_.loginEmail=? 
11:27:51,878 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [1] as [VARCHAR] - [email protected] 
11:27:52,009 INFO hsqldb.db.HSQLDB39E3504C79.ENGINE:? - Database closed 
11:27:52,225 DEBUG org.hibernate.SQL:111 - select userimpl0_.id as id1_, userimpl0_.creationDate as creation2_1_, userimpl0_.lastModified as lastModi3_1_, userimpl0_.city as city1_, userimpl0_.country as country1_, userimpl0_.latitude as latitude1_, userimpl0_.longitude as longitude1_, userimpl0_.postalCode as postalCode1_, userimpl0_.street as street1_, userimpl0_.birthDate as birthDate1_, userimpl0_.firstName as firstName1_, userimpl0_.secondName as secondName1_, userimpl0_.sex as sex1_, userimpl0_.surName as surName1_, userimpl0_.alias as alias1_, userimpl0_.enabled as enabled1_, userimpl0_.loginEmail as loginEmail1_, userimpl0_.password as password1_, userimpl0_.preferences_id as prefere19_1_ from TBL_USER userimpl0_ where userimpl0_.alias=? 
11:27:52,230 TRACE org.hibernate.type.descriptor.sql.BasicBinder:81 - binding parameter [1] as [VARCHAR] - Bladositto 
11:27:52,346 INFO hsqldb.db.HSQLDB39E3504C79.ENGINE:? - Database closed 
11:27:52,470 INFO model.hibernate.UserServiceImpl:50 - User[id: null loginEmail: [email protected] alias: Bladositto password: asdasd] class type:class model.hibernate.UserImpl$$EnhancerByCGLIB$$536b42dd 
11:27:52,481 INFO helpers.EntityAuditor:13 - User[id: null loginEmail: [email protected] alias: Bladositto password: asdasd] 
11:27:52,483 INFO helpers.EntityAuditor:13 - User[id: null loginEmail: [email protected] alias: Bladositto password: asdasd] 
11:27:52,581 DEBUG org.hibernate.SQL:111 - insert into TBL_USER (id, creationDate, lastModified, city, country, latitude, longitude, postalCode, street, birthDate, firstName, secondName, sex, surName, alias, enabled, loginEmail, password, preferences_id) values (default, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 
11:27:52,587 TRACE org.hibernate.type.descriptor.sql.BasicBinder:70 - binding parameter [1] as [TIMESTAMP] - <null> 
11:27:52,589 TRACE org.hibernate.type.descriptor.sql.BasicBinder:70 - binding parameter [2] as [TIMESTAMP] - <null> 
11:27:52,590 TRACE org.hibernate.type.descriptor.sql.BasicBinder:70 - binding parameter [3] as [VARCHAR] - <null> 
11:27:52,604 TRACE org.hibernate.type.descriptor.sql.BasicBinder:70 - binding parameter [4] as [VARCHAR] - <null> 
11:27:52,606 TRACE org.hibernate.type.descriptor.sql.BasicBinder:70 - binding parameter [5] as [DOUBLE] - <null> 
11:27:52,609 TRACE org.hibernate.type.descriptor.sql.BasicBinder:70 - binding parameter [6] as [DOUBLE] - <null> 
11:27:52,610 TRACE org.hibernate.type.descriptor.sql.BasicBinder:70 - binding parameter [7] as [VARCHAR] - <null> 
11:27:52,611 TRACE org.hibernate.type.descriptor.sql.BasicBinder:70 - binding parameter [8] as [VARCHAR] - <null> 
11:27:52,612 TRACE org.hibernate.type.descriptor.sql.BasicBinder:70 - binding parameter [9] as [DATE] - <null> 
11:27:52,614 TRACE org.hibernate.type.descriptor.sql.BasicBinder:70 - binding parameter [10] as [VARCHAR] - <null> 
11:27:52,615 TRACE org.hibernate.type.descriptor.sql.BasicBinder:70 - binding parameter [11] as [VARCHAR] - <null> 
11:27:52,616 DEBUG org.hibernate.type.EnumType:136 - Binding null to parameter: 12 
11:27:52,618 TRACE org.hibernate.type.descriptor.sql.BasicBinder:70 - binding parameter [13] as [VARCHAR] - <null> 
11:27:52,619 TRACE org.hibernate.type.descriptor.sql.BasicBinder:70 - binding parameter [14] as [VARCHAR] - <null> 
11:27:52,621 TRACE org.hibernate.type.descriptor.sql.BasicBinder:70 - binding parameter [15] as [BIT] - <null> 
11:27:52,621 TRACE org.hibernate.type.descriptor.sql.BasicBinder:70 - binding parameter [16] as [VARCHAR] - <null> 
11:27:52,623 TRACE org.hibernate.type.descriptor.sql.BasicBinder:70 - binding parameter [17] as [VARCHAR] - <null> 
11:27:52,625 TRACE org.hibernate.type.descriptor.sql.BasicBinder:70 - binding parameter [18] as [BIGINT] - <null> 
11:27:52,767 INFO hsqldb.db.HSQLDB39E3504C79.ENGINE:? - Database closed 

回答

1

「未知實體」位是因爲默認情況下Hibernate的只是看起來在類你傳遞對象的Hibernate有沒有關於這個春天,增強類的想法。在Hibernate而言,你需要註冊一個org.hibernate.EntityNameResolver這是合同,允許自定義實體的名稱解析。真正春天或許應該對其進行註冊,但那是一個小細節;)

或者你可以調用保存的形式()等。人。接受實體名稱。例如,而不是:

getHibernateTemplate().save(user); 

用這個代替:

getHibernateTemplate().save(User.class.getName(), user); 

(假設Spring的HibernateTemplate的實際暴露了這些方法,這實際上是給我的經驗與他們的模板類相當大的假設)。

+0

是的,但這個問題(由ScopedBeanInterceptor)已經解決了。關於主要問題的任何想法? – Bladositto

+0

不知道。聽起來更像是Spring增強實體的一個問題。我知道Hibernate,而不是Spring ... –