Ver Mensaje Individual
  #1 (permalink)  
Antiguo 05/07/2011, 21:21
y0mism0
 
Fecha de Ingreso: diciembre-2007
Mensajes: 135
Antigüedad: 17 años
Puntos: 1
Problema Hibernate al actualizar fila en relación OneToMany

Buenas, necesito ayuda con Hibernate. Tengo 2 entidades, Student y Phone, y hay una relación One to Many entre ellas, estas son las clases:

Código:
@Entity  
@Table(name = "STUDENT")  
public class Student {  
  
    private long studentId;  
    private String studentName;  
    private Set<Phone> studentPhoneNumbers = new HashSet<Phone>(0);  
  
    public Student() {  
    }  
  
    public Student(String studentName, Set<Phone> studentPhoneNumbers) {  
        this.studentName = studentName;  
        this.studentPhoneNumbers = studentPhoneNumbers;  
    }  
  
    @Id  
    @GeneratedValue  
    @Column(name = "STUDENT_ID")  
    public long getStudentId() {  
        return this.studentId;  
    }  
  
    public void setStudentId(long studentId) {  
        this.studentId = studentId;  
    }  
  
    @Column(name = "STUDENT_NAME", nullable = false, length = 100)  
    public String getStudentName() {  
        return this.studentName;  
    }  
  
    public void setStudentName(String studentName) {  
        this.studentName = studentName;  
    }  
  
    @OneToMany (mappedBy="student", cascade = CascadeType.ALL)        
  
    public Set<Phone> getStudentPhoneNumbers() {  
        return this.studentPhoneNumbers;  
    }  
  
    public void setStudentPhoneNumbers(Set<Phone> studentPhoneNumbers) {  
        this.studentPhoneNumbers = studentPhoneNumbers;  
    }  
    
}
Código:
      
    @Entity  
    @Table(name = "PHONE")  
    public class Phone {  
      
        private long phoneId;  
        private String phoneType;  
        private String phoneNumber;  
      
        private Student student;  
        private Set<Student> students = new HashSet<Student>(0);  
          
        public Phone() {  
        }  
      
        public Phone(String phoneType, String phoneNumber) {  
            this.phoneType = phoneType;  
            this.phoneNumber = phoneNumber;  
        }  
          
        @Id  
        @GeneratedValue  
        @Column(name = "PHONE_ID")  
        public long getPhoneId() {  
            return this.phoneId;  
        }  
      
        public void setPhoneId(long phoneId) {  
            this.phoneId = phoneId;  
        }  
      
        @Column(name = "PHONE_TYPE", nullable = false, length=10)  
        public String getPhoneType() {  
            return this.phoneType;  
        }  
      
        public void setPhoneType(String phoneType) {  
            this.phoneType = phoneType;  
        }  
          
        @Column(name = "PHONE_NUMBER", nullable = false, length=15)  
        public String getPhoneNumber() {  
            return this.phoneNumber;  
        }  
      
        public void setPhoneNumber(String phoneNumber) {  
            this.phoneNumber = phoneNumber;  
        }  
          
        @ManyToOne(cascade = CascadeType.ALL)  
        @JoinColumn (name="STUDENT_ID")  
        public Student getStudent() {  
            return this.student;  
        }  
      
        public void setStudent(Student student) {  
            this.student = student;  
        }
Aquí está el código de applicationContext.xml:

Código:
    ...  
     <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">    
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>    
            <property name="url" value="jdbc:mysql://localhost/Project"/>    
            <property name="username" value="root"/>    
            <property name="password" value="root"/>    
        </bean>    
            
        <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">    
            <property name="dataSource" ref="myDataSource" />    
            <property name="annotatedClasses">    
                <list>    
                    <value>com.domain.Student</value>                         
                    <value>com.domain.Phone</value>                           
                </list>    
            </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">create</prop>    
                </props>    
            </property>    
        </bean>    
      
     <bean id="myClassDAO" class="com.project.dao.ClassDAOImpl">  
            <property name="sessionFactory" ref="mySessionFactory"/>  
     </bean>   
      
    ...
Y ésta es la clase ClassDAOImpl.

Código:
    public class ClassDAOImpl{  
      
    private HibernateTemplate hibernateTemplate;  
    private Session session;  
    public void setSessionFactory(SessionFactory sessionFactory) {  
            this.hibernateTemplate = new HibernateTemplate(sessionFactory);  
            this.session = sessionFactory.openSession();  
    }     
      
    public void updateStudent(){  
              
            //update using hibernateTemplate  
      
            Student s = (Student)hibernateTemplate.get(Student.class, new Long(1));    
            Set<Phone> phoneNumbers =s.getStudentPhoneNumbers();    
            phoneNumbers.add(new Phone ("house","12342342"));    
            s.setStudentPhoneNumbers(phoneNumbers);    
            hibernateTemplate.update(s);    
      
            /* 
            //update using session 
     
            Transaction transaction = session.beginTransaction();           
            Student s = (Student)session.get(Student.class, new Long(1));   
            Set<Phone> phoneNumbers =s.getStudentPhoneNumbers();   
            phoneNumbers.add(new Phone ("house","12342342"));   
            s.setStudentPhoneNumbers(phoneNumbers);   
            session.update(s);   
            transaction.commit();   
            */  
      
    }  
      
    }
En esta clase lo único que pretendo es actualizar la lista de teléfonos del estudiante, añadiéndole uno más.
Me crea una nueva fila en la tabla Phone, con el nuevo teléfono, hasta ahí bien. El problema está en que no me crea la relación, la columna de join de la tabla phone (STUDENT_ID) parace con valor null, cuando debería aparecer con valor "1".

¿alguien sabe cómo puedo solucionar esto?


Gracias y un saludo