-->
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: JPA like mapping with Hibernate
PostPosted: Tue May 27, 2008 8:33 am 
Newbie

Joined: Thu May 22, 2008 2:16 pm
Posts: 9
Hi , I would like to start Hibernate 3 in my university term project. I have tried to map hibernate and my pojo classesses by using hibernate annotations.

At this point, I don't know that is this approach(using EJB3 like annotations) has a negative effect for using full hibernate support in my application. For example, can I use hibernate HQL or other full hibernate capabilities, criterias... ?

Thank you for everyone...
anut


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 10:03 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Well, you've certainly come to the right place for answers! We'll do our best to make your Hibernate and JPA experience as seamless as possible. You'll do great on your assignments!

Take a look at the following object model:

Image

This includes all of your basic one-to-one, one-to-many and many-to-many associations, not to mention a Client class with a whole whack of associations in it. But taken one at a time, it's incredibly easy to map all of these with JPA annotations. While it may look a little intimidating, here's the whole Client class annotated with JPA:

Code:
  package com.examscam.model;
  import java.util.*;import javax.persistence.*;
  @Entity
  @Table(name = "client", schema = "examscam")
  public class Client {
  private List<Address> addresses = new Vector<Address>();
  private List<Skill> skills = new Vector<Skill>();
  private ClientDetail clientDetail;
  private Long id;private String username;
  private String password;private Boolean verified;
  @Id
  @GeneratedValue
  @Column(name = "id")
  public Long getId() {return id;}
  public void setId(Long id) {this.id = id;}
  @ManyToMany
  @JoinTable(name = "client_skill",
  joinColumns = { @JoinColumn(name = "client_id") },
  inverseJoinColumns = { @JoinColumn(name = "skill_id") })
  public List<Skill> getSkills() {return skills;}
  public void setSkills(List<Skill> skills){this.skills=skills;}   
  @OneToMany(mappedBy="client", targetEntity=Address.class,   
  fetch=FetchType.EAGER, cascade = CascadeType.ALL)
  public List<Address> getAddresses() {return addresses;}
  public void setAddresses(List<Address> addresses) {
  this.addresses = addresses;
  }
  @OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
  @JoinColumn(name="detail_id")
  public ClientDetail getClientDetail(){return clientDetail;}
  public void setClientDetail(ClientDetail clientDetail) {
  this.clientDetail = clientDetail;
  }
  public String getPassword() {return password;}
  public void setPassword(String password){this.password = password;}
  public String getUsername() {return username;}
  public void setUsername(String username) {
  this.username = username;
  }
  public Boolean getVerified() {return verified;}
  public void setVerified(Boolean verified){this.verified=verified;}
  }


***Java indentation is highly overrated. ;)***

As you can see, it's easy to annotated very complicated relationships. And from here, the whole Hiberante arsenal is available to you. You can do HQL, Criteria Queries, even throw in some Hibernate specific annotation from time to time. Plus, you don't have to worry too much about xml files getting out of sync.

You can find more on this in some of the Hibernate tutorials and JPA examples on my website:

http://www.thebookonhibernate.com/HiberBookWeb/learn.jsp?tutorial=20advancedentitymapping

Good luck!

_________________
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  
 
 Post subject:
PostPosted: Tue May 27, 2008 1:29 pm 
Newbie

Joined: Thu May 22, 2008 2:16 pm
Posts: 9
Nice to hear that. Thank you for your well suited example.

Helpful

anut


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.