-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Updating Entity with Collection
PostPosted: Wed Oct 13, 2010 3:49 pm 
Newbie

Joined: Thu Aug 19, 2010 3:36 pm
Posts: 3
Hi All,

I am fairly new to Hibernate. I have what seems like a fairly simple question. I need to understand how to update an entity that has an association with another entity.

I have two entities User and Role entity. User has certain fields, firstName, lastName, etc... I am able to display these to the user using Spring MVC and they are able to update them and it works fine. The problem comes when I added the association to the Role entity and a field of type List called "Roles" to the User entity. I am able to display this to the user using checkboxes but when they select a role and hit submit I get a class cast exception. Specifically...

Code:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:343)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:149)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)

root cause

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String
    org.hibernate.type.StringType.toString(StringType.java:44)
    org.hibernate.type.NullableType.toLoggableString(NullableType.java:168)
    org.hibernate.type.EntityType.toLoggableString(EntityType.java:471)
    org.hibernate.type.CollectionType.renderLoggableString(CollectionType.java:160)
    org.hibernate.type.CollectionType.toLoggableString(CollectionType.java:144)
    org.hibernate.pretty.Printer.toString(Printer.java:53)
    org.hibernate.pretty.Printer.toString(Printer.java:90)
    org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:97)
    org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
    org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
    $Proxy35.flush(Unknown Source)
    com.edusoft.inote.dao.UserDAOImpl.editUser(UserDAOImpl.java:39)
    com.edusoft.inote.service.UserManagerImpl.editUser(UserManagerImpl.java:59)
    com.edusoft.inote.web.UserFormController.editProfile(UserFormController.java:164)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:343)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:149)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)


User Entity
Code:
@Entity 
@Table(name="users")
public class User implements Serializable{

    private String username;

    private String password;
   
    private String firstName;

    private String lastName;
   
    private String confirmpassword;

    private int enabled;
   
    private String authtoken;
   
    private List<Roles> roles;

   
///////////// Getters /////////////////   
    @Id
    @Column (name = "username", length=50)
    @NotEmpty(message = "{user.username.blank}")
    @Email(message = "{user.username.valid}")
    public String getUsername() {
        return username;
    }
    @NotEmpty(message = "{user.password.blank}")
    @Column (name = "password", length=50)
    public String getPassword() {
        return password;
    }
    @Transient
    public String getConfirmpassword() {
        return confirmpassword;
    }
    @Column (name = "firstname", length=50)
    @NotEmpty(message = "{user.firstname.blank}")
    public String getFirstName() {
        return firstName;
    }
    @Column (name = "lastname", length=50)
    @NotEmpty(message = "{user.lastname.blank}")
    public String getLastName() {
        return lastName;
    }
    @Column (name = "enabled", length=50)
    public int getEnabled() {   
        return enabled;
    }

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "user_role",
       joinColumns = { @JoinColumn(name = "userid") },
       inverseJoinColumns = { @JoinColumn(name = "roleid") })
    public List<Roles> getRoles() {
        return roles;
    }
   
    /*
    @OneToMany(targetEntity=Authority.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER )
    @JoinColumn(name="username")
    public List<Authority> getAuthorities() {
        return authorities;
    }
    */
   
    @Column (name="authtoken", length=50)
    public String getAuthtoken() {
        return authtoken;
    }
////////////////// Setters ///////////////////////
    public void setUsername(String userName) {
        this.username = userName;
    }
    public void setPassword(String password){
        this.password = password;
    }   
    public void setFirstName(String name) {
        this.firstName = name;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public void setEnabled(int enabled) {
        this.enabled = enabled;
    }
    public void setConfirmpassword(String confirmpassword) {
        this.confirmpassword = confirmpassword;
    }
   
    public void setRoles(List<Roles> roles){
        this.roles = roles;
    }
    /*
    public void setAuthorities(List authority) {
        this.authorities = authority;
    }
    */
    public void setAuthtoken(String authtoken) {
        this.authtoken = authtoken;
    }
}


Roles
Code:
@Entity 
@Table(name="roles")
public class Roles {

   private String id;
   private String role;
    private List<User> users;
   
   public Roles(){

   }
   
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
   public String getId() {
      return id;
   }
   public void setId(String id) {
      this.id = id;
   }
   @Column (name = "role", length=50)
   public String getRole() {
      return role;
   }
   public void setRole(String role) {
      this.role = role;
   }
   @ManyToMany(mappedBy = "roles")
   public List<User> getUsers() {
      return users;
   }
   public void setUsers(List<User> users) {
      this.users = users;
   }   
   
}


DAO Method
Code:
    public void editUser(User user) {
        logger.info("UserDAOImpl:editUser "+user.getUsername());
        Session session = HibernateFactory.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();       
        session.update(user);
        session.flush();
        tx.commit();
        super.closeSession(session);
    }


JSP/View
Code:
         <tr>
            <td>Roles :</td>
            <td><form:checkboxes path="roles" items="${rolesList}" itemValue="id" itemLabel="role" /></td
         </tr>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.