-->
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.  [ 3 posts ] 
Author Message
 Post subject: BUG? Field based validation and lazy initialized entities
PostPosted: Thu Jul 17, 2008 12:17 pm 
Newbie

Joined: Sat Jul 12, 2008 4:25 am
Posts: 8
We are experienced a strane bug when using field based validation with lazily initialized entities. I have an Employee entity which is contained in an EmployeeGroup. Our code looks like:


Code:
@Entity
public class Employee {
   @Id
   private int id;
   
   @Valid
   @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
   private EmployeeGroup group;
   
    private String name;

    public Employee() {
      super();       
    }
   
   public Employee(int id) {
      super();
      this.id = id;
   }

   public EmployeeGroup getGroup() {
      return group;
   }

   public void setGroup(EmployeeGroup group) {
      this.group = group;
   }

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

Code:
@Entity
public class EmployeeGroup {
   @Id
   private int id;
   
   @NotNull
   private String name;
   
   @OneToMany(mappedBy = "group", fetch = FetchType.LAZY)
   private Set<Employee> employees = new HashSet<Employee>();

   
   public EmployeeGroup() {
      super();      
   }
   
   public EmployeeGroup(int id, String name, Set<Employee> employees) {
      super();
      this.id = id;
      this.name = name;
      this.employees = employees;
   }

   public Set<Employee> getEmployees() {
      return employees;
   }

   public void setEmployees(Set<Employee> employees) {
      this.employees = employees;
   }

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}


Our test code looks like:
Code:
   @Override
   protected void onSetUpBeforeTransaction() throws Exception {
      super.onSetUpBeforeTransaction();
      startNewTransaction();
      EntityManager entityManager = createContainerManagedEntityManager();
      employee = new Employee(EMPLOYEE_ID);
      employees.add(employee);
      employeeGroup = new EmployeeGroup(EMPLOYEE_GROUP_ID, GROUP, employees);
      employee.setGroup(employeeGroup);
      entityManager.persist(employeeGroup);
      entityManager.persist(employee);
      setComplete();
      endTransaction();
   }
       
    public void testSaveChangedEmployee()  throws Exception {
      EntityManager entityManager = createContainerManagedEntityManager();
      Employee findEmployee = entityManager.find(Employee.class, EMPLOYEE_ID);
      assertNotNull(findEmployee);
      findEmployee.setName("Hans");
      findEmployee.getGroup().getEmployees().add(findEmployee);
      entityManager.persist(findEmployee);
      setComplete();
      endTransaction();
    }


With this test code we're getting an Validationexception because the validator accesses the employe group proxies fields (which are always null) via refelection directly instead of using the getter methods. When I move the validation annotations to the getter methods everything works fine. So we have a workaround for the problem, but to us this strange behavior looks like breaking the transparency of using lazy initialized entities. One might even consider this as a bug! What do you think?

Best regards,
Matthias


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 18, 2008 11:30 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Unfortunately it is a problem quite hard to solve. If you have a solution, I am taker.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 20, 2008 3:46 pm 
Newbie

Joined: Sat Jul 12, 2008 4:25 am
Posts: 8
Hi Emmanuel,

Why is it so difficult? I thought Hibernate provides methods to check if an object is a proxy (at least Hibernate.getClass(a).equals(a.getClass()) should work;-). And Hibernate Annotations depends on Hibernate Core, right? So calling Hibernate.getClass should be possible... Can you perhaps explain why it's difficult to fix this behavior? Or what is the design rationale behind annotations on fields and methods? And we even haven't found any hint (neither via your really good documentation nor via google;-) that field validation doesn't work with lazily initialized entities... How will validation annotations work in the new Bean Validation JSR?

A curious
Matt


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

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.