OK... so it might help to see the code from your GenericSearchDaoHibernate class, but I it works just fine for me with the following:

Assuming you checked out from the following (which by the way, the suggested way for using appfuse 2 is not to use the code directly as you did with appfuse 1, but do use an archetype, and depend on appfuse in your pom.xml) :

https://appfuse.dev.java.net/svn/appfuse/trunk/

1)

Add this to your User class in /data/common

   protected Set<Nickname> nickNames = null;
@OneToMany(mappedBy="user", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
   public Set<Nickname> getNickNames() {
       return nickNames;
   }

   public void setNickNames(Set<Nickname> nickNames) {
       this.nickNames = nickNames;
   }


2) Create the Nickname class in /data/common (file attached)

3) Add the following to /data/hibernate/src/test/resources/hibernate.cfg.xml

<mapping class="org.appfuse.model.Nickname"/>

4) Add the test src/test/java/org/appfuse/dao/UserNicknameDaoTest.java (file attached) to /data/hibernate

Notice here that I didn't create a new dao or method or anything. Simply used the GenericDao that is already configured for the User class, and by virtue of the fact that we mapped nicknames with an EAGER fetch type, the 'get' works for us.

5) Run mvn:install from /data/common

6) Run mvn:test from /data/hibernate - this should create your nicknames table

7) Insert 3 rows into the nicknames table that refer to user with id of 1 (more or less than 3 and the test will not pass)

8) Run 'mvn -Dtest=UserNicknameDaoTest' from /data/hibernate - the test should pass and you should see something like this from the println's

The story behind 'Country Bry' is : 'Sean and Raible put their heads together for this one.'
The story behind 'Country' is : 'Hef shortened Country Bry to this.'
The story behind 'Meat' is : 'Filios gave me this one.'



Let me know if something doesn't work.



ros wrote:
(Appguse2 Hibernate)
I've added OneToMany relation with FetchType.EAGER from User pojo to Test
pojo.
Now GenericDaoHibernate getAll returns 5 users, but in database there are 2
users - one amin user and one tomcat user, tomcat user has relation to 4
Test pojos.
Can getAll return distinct selection of users?




@Entity
@Table(name="app_user")
public class User extends BaseObject implements Serializable, UserDetails {
...
    protected Set<Test> tests = new HashSet<Test>();
...
    @OneToMany(targetEntity=Test.class, cascade=CascadeType.ALL,
mappedBy="user", fetch = FetchType.EAGER)
    public Set<Test> getTests() {
        return testss;
    }
...
}

test:

GenericSearchDaoHibernate<User, Long> searchDao = new
GenericSearchDaoHibernate(User.class);
searchDao.setSessionFactory((SessionFactory)
applicationContext.getBean("sessionFactory"));
List<User> users = searchDao.getAll(); System.out.println(users.toString());

output:
[EMAIL 
PROTECTED],enabled=true,accountExpired=false,credentialsExpired=false,accountLocked=false,Granted
Authorities: ,user],
[EMAIL 
PROTECTED],enabled=true,accountExpired=false,credentialsExpired=false,accountLocked=false,Granted
Authorities: ,user],
[EMAIL 
PROTECTED],enabled=true,accountExpired=false,credentialsExpired=false,accountLocked=false,Granted
Authorities: ,user],
[EMAIL 
PROTECTED],enabled=true,accountExpired=false,credentialsExpired=false,accountLocked=false,Granted
Authorities: ,user],
[EMAIL 
PROTECTED],enabled=true,accountExpired=false,credentialsExpired=false,accountLocked=false,Granted
Authorities: ,admin]]


If relation is FetchType.LAZY then no duplicate users, but I need to load
Test pojos with user.
package org.appfuse.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

@Entity
@Table(name="nicknames")
public class Nickname extends BaseObject {
    
    protected Long id;    
    protected String name;
    protected String storyBehindName;        
    protected User user;
    
    @Id  @GeneratedValue(strategy=GenerationType.AUTO)
    public Long getId() {
        return id;
    }
    
    @Column(length=30)
    public String getName() {
        return name;
    }
    
    @Column(length=400)
    public String getStoryBehindName() {
        return storyBehindName;
    }
    
    @ManyToOne(cascade=CascadeType.ALL) @JoinColumn(name="user_id_ref")
    public User getUser() {
        return user;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setStoryBehindName(String storyBehindName) {
        this.storyBehindName = storyBehindName;
    }
    public void setUser(User user) {
        this.user = user;
    }
    
    /**
     * @see java.lang.Object#equals(Object)
     */
    public boolean equals(Object object) {
        if (!(object instanceof Nickname)) {
            return false;
        }
        Nickname rhs = (Nickname) object;
        return new EqualsBuilder().append(
        	this.user, rhs.user).append(this.storyBehindName,
        	rhs.storyBehindName).append(this.name, rhs.name).append(
        	this.id, rhs.id).isEquals();
    }
    /**
     * @see java.lang.Object#hashCode()
     */
    public int hashCode() {
        return new HashCodeBuilder(-288934289, 311543415).append(this.user)
        	.append(this.storyBehindName).append(this.name).append(this.id)
        	.toHashCode();
    }
    /**
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return new ToStringBuilder(this).append("name", this.name).append(
        	"storyBehindName", this.storyBehindName).append("id", this.id)
        	.append("user", this.user).toString();
    }
    
    

    

}
package org.appfuse.dao;

import org.appfuse.Constants;
import org.appfuse.model.Address;
import org.appfuse.model.Nickname;
import org.appfuse.model.Role;
import org.appfuse.model.User;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;

public class UserNicknameDaoTest extends BaseDaoTestCase {
    private UserDao dao = null;
    
    public void setUserDao(UserDao dao) {
        this.dao = dao;
    }

    public void testGetUserInvalid() throws Exception {
        try {
            dao.get(1000L);
            fail("'badusername' found in database, failing test...");
        } catch (DataAccessException d) {
            assertTrue(d != null);
        }
    }

    public void testGetUser() throws Exception {
        User user = dao.get(1L);

        assertNotNull(user);
        assertEquals(1, user.getRoles().size());        
        assertTrue(user.isEnabled());
        
        super.assertEquals(3, user.getNickNames().size());
        for (Nickname n : user.getNickNames()) {
            System.out.println("The story behind '" + n.getName() + "' is : '"  + n.getStoryBehindName() + "'");
        }
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to