4 Replies Latest reply on Aug 14, 2012 3:47 PM by lukasw44

    Springmvc , problem with generics dao

    lukasw44

      Hello i have got wery big problem and plz help me it is first time when junit test its fine but if i use function in real program its not working (none errors)

       

      i create very simple GenericsDAO like that:

       

      public abstract class GenericDAO <T , PK extends Serializable> implements IGenericDAO<T, PK>
      {
          
          @Autowired
          protected EntityManager entityManager;
      
          protected Class<T> clazz;
          
          public GenericDAO(final Class<T> clazz)
          {
              this.clazz = clazz;
          }
          
          @Override
          public void save(final T entity)
          {
              entityManager.persist(entity);
          }
          
          @Override
          public void delete(final T entity)
          {
              entityManager.remove(entity);
          }
      }
      

       

      It is very very simple generics dao as you see its one constructor with 1 attribute , next class must overwrite ...

       

      and i use standard simple example with member standard quickstart project in jboss

       

       

      @Repository
      @Transactional
      public class MemberDaoImpl extends GenericDAO<Member, Long> implements MemberDao
      {
          
          //default constructor start when object created
          public MemberDaoImpl() {
              super(Member.class);
          }
      
        //register first function persist
          @Override
          public void register(Member member)
          {
              entityManager.persist(member);
              return;
          }
      

       

      as you see this class have got 1 default constructor and initialize superclass with element Memver.class

       

      And its very strange because my junit like that :

       

       

         
          //memberDao.register(member);
          //used method with generics save the same function as resister
          memberDao.save(member);

       

      working fine but if i try use this function save in MemberController :

       

       

        @RequestMapping(method=RequestMethod.POST)
          public String registerNewMember(@Valid @ModelAttribute("newMember") Member newMember, BindingResult result, Model model)
          {
              if (!result.hasErrors()) {
                  
                  //memberDao.register(newMember);
                  //we used generics method save 
                  memberDao.save(newMember);
                  //save is no working why ???
      

       

      Its not working why ??

      plz its very interesting for me because its first time when i used junit and everything is fine byt if i try use function in real app its problem :/

       

      additional info ;

      i used Jboss as 7.1 Thunder

       

      I attach very very simple project quickstart spring mvc with aditional generics dao and new test plz told me what is wrong , :/ i havent got any idea i spend 2 days and dont know :/...

        • 1. Re: Springmvc , problem with generics dao
          sfcoy

          {code:java}@Transactional

          public abstract class GenericDAO <T , PK extends Serializable> implements IGenericDAO<T, PK>{code}

           

          Personally, I don't use DAO style layers anymore. The EntityManager is effectively a DAO anyway and if you're performing non-trivial operations on multiple tables you'll find yourself building a service class that looks like a christmas tree covered in DAOs.

           

          YMMV

          • 2. Re: Springmvc , problem with generics dao
            lukasw44

            So you say that GenericsDAO isnt good idea...

            but its a lot of articles and tutorial where you find different opinion ...

             

            anyways its interesting that my junit test are passed but in real app its not working .. but my boss and all programers says : if your test are fine so everything is good :/

            • 3. Re: Springmvc , problem with generics dao
              sfcoy

              Have a look at DAOS AREN'T DEAD - BUT THEY EITHER COLLAPSED OR DISAPPEARED. I think Gavin King has made similar remarks although I can't find the reference right now.

               

              That fact that your unit test works and application does not may be a consequence of a JPA integration bug that I recall being mentioned here sometime back. I believe that the EntityManager should have still executed your data operation, even in the absence of an explicit transaction (which is not a recommended scenario BTW). Try it out on one of the nightly builds.

              • 4. Re: Springmvc , problem with generics dao
                lukasw44

                Thanks yout very much