Preventing duplicate elements in foreign key column
===

Using Hibernate 3.1 and have two mapping files m_devotee and m_member with classes M_devotee and M_member. I want to avoid duplicate entries from m_devotee column devotee_id which is referenced as devotee_id foreign key in m_member table.

I'm new to Hibernate and want to know if I'm missing some tags in the mappings.

Here are my files, https://gist.github.com/2e2560e7a04a769848a4

For now, I tried to prevent duplicates by adding code in my dao file. It gives me a ClassCastException. It should also rollback the transaction and return false to the servlet so it does not forward it to other jsp file.



    Query qr1=session.createQuery("select m.devotee_id from M_member m");  
    List<M_member> l1=qr1.list();  
    for(M_member mi:l1)  
    {  
     if(!(l1.contains(member.getDevotee_id())))  
     {  
      member.setDevotee_id(member.getDevotee_id());  
     //System.out.println("print devotee id"+member.getDevotee_id());  
     }  
    }   



M_devotee object cannot be cast to M_member as seen here.

I'm lost and any help is thoroughly appreciated! 