-->
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: New to Hibernate: java map with HQL
PostPosted: Sun Feb 06, 2011 12:59 pm 
Newbie

Joined: Sun Feb 06, 2011 12:42 pm
Posts: 2
Hi
before i begin i know that here are already some questions regarding the java.util.map but i spend an hour and i cant seem to find the answer...

Before i begin here is the code i have written so far:
This is the entity:
Code:
@Entity
public class StoryGroup implements Serializable, IStoryGroup {
    private static final long serialVersionUID = 1L;
    private int               id;
    private String            groupName;
    private Set<IStory>       stories;

    StoryGroup() {
    }

    public StoryGroup(final String name) {
        groupName = name;
        createHashCode();
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId() {
        return id;
    }

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

    @Lob()
    @Column(name = "GroupName")
    public String getName() {
        return groupName;
    }

    public void setName(final String groupName) {
        this.groupName = groupName;
        createHashCode();
    }

    // bi-directional many-to-one association to Story
    @OneToMany(mappedBy = "group", targetEntity = Story.class)
    public Set<IStory> getStories() {
        return stories;
    }

    public void setStories(final Set<IStory> stories) {
        this.stories = stories;
    }

    private int createHashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (groupName == null ? 0 : groupName.hashCode());
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final StoryGroup other = (StoryGroup) obj;
        return GeneralUtil.isEqual(groupName, other.groupName);
    }

    /** {@inheritDoc} */
    @Override
    public int hashCode() {
        return groupName.hashCode();
    }

}


This is a util class that shall contain all elements from the data base:
Code:
public final class StoryGroups {
    private final Map<String, IStoryGroup> groups;

    /**
     *
     */
    public StoryGroups() {
        final Session s = SessionProvider.getSessionFactory().openSession();
        final List<?> q = s
                .createQuery("select sg.name as key, sg.name as value from StoryGroup as sg") //<---------------???
                .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list(); //<---------------???

        groups = null;   //<---------------???
        s.close();
    }

    public IStoryGroup getGroupByName(final String name) {
        IStoryGroup grp = groups.get(groups.indexOf(name));
        if (grp == null) {
            grp = new StoryGroup(name);
            final Session s = SessionProvider.getSessionFactory().openSession();
            final Transaction t = s.beginTransaction();
            t.begin();
            s.save(grp);
            t.commit();
            s.close();
        }
        return grp;
    }

    public static void main(final String[] args) {
        final StoryGroups s = new StoryGroups();
        final IStoryGroup g1 = s.getGroupByName("Fiction");
        final IStoryGroup g2 = s.getGroupByName("Biography");
        System.out.println(g2);
    }
}


Basically i want the result of the query to be put into that map so that i can get a StoryGroup directly by its name.

So what do i have to do to get a String ("name") as key and a StoryGroup entity as value?

BTW: This is experimental code, so excuse the style please :)

Thank you! :)


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.