-->
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: Mapping single column of forign table to String[]
PostPosted: Wed Apr 25, 2007 4:55 pm 
Newbie

Joined: Thu Feb 03, 2005 9:19 pm
Posts: 11
Location: Bedford, TX
I'm using Hibernate Annotations 3.3.0 and Hibernate Core 3.2.3. I have two very simple tables; a users table and a user_roles table. Each user has one or more user_roles as follows:

Code:
create table users (
  id varchar(6) not null primary key,
  last_active_date date,
  .... etc. ...
);

create table user_roles (
  user_id varchar(6),
  role varchar(10)
);


user_roles.user_id is a foreign key to the users table.

I have a User class which maps to the users table and I would like to have a Java property "String[] roles" instead of "UserRoles[] roles" where I define a Java class for the user_roles table when I only need one field out of it. Abbreviated example:

Code:

@Entity
@Table(name = "users")
// Something goes here?
public class User {
    private String id;

    private Date lastActive;

    private String[] roles;

    @Id
    @Column(length = 6)
    public String getId() {
        return id;
    }

    @Column(name = "last_active", nullable = false)
    public Date getLastActive() {
        return lastActive;
    }

    // what goes here??
    public String[] getRoles() {
        return roles;
    }
}



Can this be done? I know it's nit-picky, I just hate the idea of creating a separate class when I only need one field out of this table. Plus, my brain counts the bytes of memory an additional two objects for each user role will consume (I can't seem to let go of my "4KB of RAM" days).

Thanks,
Daniel


Top
 Profile  
 
 Post subject: More info
PostPosted: Thu Apr 26, 2007 3:42 am 
Newbie

Joined: Thu Feb 03, 2005 9:19 pm
Posts: 11
Location: Bedford, TX
Ok, after a lot of scouring, I've discovered the Hibernate CollectionOfElements annotation. But I have one more question yet! (both for somebody who wants a point and for me not wanting to waste one :). I'm noticing that it likes to eager fetch my joined data one row at a time for each row in users rather than grab them assemble them client-side. Any way around that? (i.e., so that Hibernate will make only two queries, one into the users table followed by another into the user_role_matrix table?)

Code:
@Entity
@Table(name = "users")
public class User {
    private String id;

    private Date lastActive;

    private Set<String> roles;

    @Id
    @Column(length = 6)
    public String getId() {
        return id;
    }

    @Column(name = "last_active", nullable = false)
    public Date getLastActive() {
        return lastActive;
    }

    @CollectionOfElements(fetch = FetchType.EAGER)
    @JoinTable(
        name = "user_role_matrix",
        joinColumns = @JoinColumn(name = "user_id"),
        uniqueConstraints = @UniqueConstraint(
            columnNames = {"user_id", "role"}
        )
    )
    @Column (name = "role")
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    public Set<String> getRoles() {
        return roles;
    }
}


Responses greatly appreciated.

Daniel


Top
 Profile  
 
 Post subject: RESOLVED
PostPosted: Thu Apr 26, 2007 5:27 pm 
Newbie

Joined: Thu Feb 03, 2005 9:19 pm
Posts: 11
Location: Bedford, TX
Ok, I finally figured this one out too. I needed the @BatchSize annotation.

Code:
    @CollectionOfElements(fetch = FetchType.EAGER)
    @JoinTable(
        name = "user_role_matrix",
        joinColumns = @JoinColumn(name = "user_id"),
        uniqueConstraints = @UniqueConstraint(
            columnNames = {"user_id", "role"}
        )
    )
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    @Column (name = "role")
    @BatchSize(size = 125)
    public Set<String> getRoles() {
        return roles;
    }


OK, I solved two of my own problems, can I get my point back? LOL!

Daniel


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.