-->
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.  [ 2 posts ] 
Author Message
 Post subject: Regarding mapping and code
PostPosted: Thu May 15, 2008 12:05 pm 
Newbie

Joined: Thu May 15, 2008 11:58 am
Posts: 2
Hi to all,

I am new to hibernate and myelcipse and could u pls let me know how to map two tables in Eclipse and how to retrieve data using hibernate .

As I mapped two tables using one2many relationship but I want the code how to retrieve the data using sql r hql query.

I am using myeclipse 6.0 using that only can u pls suggest me .

pls help me if u know the solution.






















Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html

[quote][/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 15, 2008 1:39 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Here's a neat little tutorial to show you how to work with one to many relationships:

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=18mappingonetomanyassociations

This example uses the simple, A Team Has Many Players idea. The annotations are straight forward:

Code:

@OneToMany(mappedBy="team", 
              targetEntity=Player.class,
    fetch=FetchType.EAGER, cascade = CascadeType.ALL)
public List<Player> getPlayers() {return players;}



Code:
@ManyToOne
@JoinColumn(name="team_id")     
public Team getTeam() {return team;}


Here's the code of how to create and save instances of Team and Player. Here I use setters, but getting the information would be as simple as doing a find or get with the primary key, and then calling the getter methods:

Code:
    HibernateUtil.recreateDatabase();
    Session session=HibernateUtil.beginTransaction();

    Team team = new Team();
    Player p1 = new Player();
    Player p2 = new Player();

    session.save(team);
    session.save(p1);
    session.save(p2);

    team.setName("Pickering Atoms");
    p1.setNickName("Lefty");
    p1.setTeam(team);
    p2.setNickName("Blinky");
    p2.setTeam(team);
    HibernateUtil.commitTransaction();


Check out my signature links for more Hibernate examples and JPA tutorials.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.