-->
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: Hibernate/JSF bean best practice and empty collections
PostPosted: Thu Dec 13, 2007 3:35 pm 
Newbie

Joined: Thu Dec 13, 2007 10:49 am
Posts: 5
Hibernate version: 3.2
Name and version of the database you are using: MySQL 5.1
Framework JSF 1.2, Tomcat 6.0.14, Unified EL

I have found Hibernate extremely easy to implement. However there are three things that I am unable to find good documentation on or user input relating to Hibernate and JSF. All of my questions are based off the following code.

Code:
//Figure 1
Public class Foo implements java.io.Serializeable {
  private Integer fooId;
  private String description;
  private Bar bar;

  public Foo() {
  }

  public Foo(String description, Bar bar) {
    this.description=description;
    this.bar=bar;
  }

  public Integer getFooId() {
    return this.fooId;
  }
 
  public void setFooId(Integer fooId) {
    this.fooId=fooId;
  }

  public String getDescription() {
    return this.description;
  }

  public void setDescription(String description) {
    this.description=description;
  }

  public Bar getBar() {
    return this.bar;
  }

  public void setBar(Bar bar) {
    this.bar=bar;
  }

}

Public class Bar implements java.io.Serializeable {
  private Integer barId;
  private String description;
  private Set foos = new HashSet(0);

  public Bar() {
  }

  public Bar(String description,Set foos) {
    this.description=description;
    this.foos=foos;
  }

  public Integer getBarId() {
    return this.barId;
  }
 
  public void setBarId(Integer barId) {
    this.barId=barId;
  }

  public String getDescription() {
    return this.description;
  }

  public void setDescription(String description) {
    this.description=description;
  }

  public Set getFoos() {
    return this.foos;
  }

  public void setFoos(Set foos) {
    this.foos=foos;
  }

}


1) When using an Unified EL expression such as #{Foo.Bar.description} is it best to instantiate the Bar oject in Foo's get so as to prevent a null reference? The objects are cascade="save-update" For example:
Code:
  public Bar getBar() {
    if (this.bar==null) {
      this.bar=new Bar()';
      this.getBar().getFoos().add(this);
    }
    return this.bar;
  }


or just initialize it in the object's constructor or variable declaration, for example.

Code:
  ...
  private String description;
  private Bar bar = new Bar();

  public Foo() {
  ...


2) When using Hibernate with JSF is it best to subclass the object when using EL? Thus turning it into a bean/backing-bean. The EL would be #{Foo.description}
with the above Fig.1 sample would be modified to:
Code:
//Figure 2
Public class FooBase implements java.io.Serializeable {
  ...
}

Public class BarBase implements java.io.Serializeable {
  ...
}

Public class Foo extends FooBase {
  //implement Bean for JSF

  ...
}

Public class Bar extends BarBase {
  //
  ...
}

or EL could be #{FooBean.foo.description} with the Fig. 1 sample plus
Code:
//Figure 3
Public class FooBean {
  public Foo foo=new Foo();
  public Foo getFoo() {
    return this.foo;
  }
  public void setFoo(Foo foo) {
    this.foo=foo;
  }

  //implement the rest of the Bean
  ...
}


I would think that subclassing is desireable from an upgradeability viewpoint.

3) Is there a way to instantiate an object when the collection is empty? Given the EL #{Bar.Foos[0]} where we have a Bar object and an empty Set Foo.
It doesn't seem right to do
Code:
//Figure 4
  public Set getFoos() {
    if (this.foos.size()==0)
    {
      this.foos.add(new Foo());
    }
    return this.foos;
  }

as we don't know what size it should be initially. I would have thought that Hibernate would take care of the instantiation of the object Foo when Bar is a new object (not in the database) and the Set of Foos was empty. If this is possible, I cannot seem to get this working with any combination of lazy loading/Hibernate.initialize and the documentation doesn't (as far as I can tell) tell me that this can be done. It may lazy load objects that only exist in the database.

If it is not right to do Figure 4 then creating a Bean to handle the page detail and object creation when necessary as shown in Figure 3 seems to be the best choice such that when an empty Set is retrieved and object is created. There would be a potential duplication of getter/setters mapping to the Hibernate objects when using it as a backing bean.


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.