-->
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.  [ 6 posts ] 
Author Message
 Post subject: hibernate and maping and sql
PostPosted: Wed Jul 23, 2008 2:57 am 
Newbie

Joined: Wed Jul 23, 2008 2:50 am
Posts: 16
Hi Every one!

can anyone describe when i have to to choose the hibernate mapping and when i have to use the hql.

and can i use hibernate mapping in all cases, i mean if i want to use where, order and so on in a statement....


I have a simple question too. if i have some like this query
select t1.c1, t1.c2 from table1 as t1, table2 as t2 where t1.c2 ="something" and t1.c1=t2.c1

How can i use the map for generate this query.
I would like use hibernate-mapping not hibernate-query or sql-query.

and is it a good way to use hibernate in this kind situation?

Thanks
Lars


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 3:13 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Wow....Easy Tiger...

That's alot of questions in there. Good questions, but sometimes one per post can make it easier to answer.

Hibernate allows you to think about your problem domain in terms of objects. Then, you map your objects that need persistence to the database layer. So, do you need to map all your objects? Well, if you want the data the contain to be persisted, then yes, you do!



Here's a little discussion on Hibernate from my website. Check out my website for some simple tutorials and information on how to get started. Get a bit of Hibernate under your belt, and keep asking question! Trust me, youll love Hibernate if you just start using it.

A Quick Discussion: What is Hibernate? and How Does it Work?


Quote:

What is Hibernate, you ask?

Well, there's a long answers to that, and there's short answers to that. The short and simple answer? Hibernate makes it easy to save data to, or load data from, a database.

And of course, Hibernate is Java based, and we all love Java. Java's object oriented, it's cross platform, it's fun to code, and it makes you think of that black blood of the soul: Tim Hortons Coffee. Seriously though, Hibernate is real easy to use, especially if you have a bit of a Java background. And it integrates quite naturally into your existing Java programs.
I keep hearing the term 'Java Persistence.' What does 'Java Persistence' mean?

When we write Java code, we create objects, and those objects have properties. Here's a simple piece of code. Just by looking at it, I think you can tell what the User's name and password are:

User user = new User(); //an object named user is created
user.setName("Cameron"); //name is initialized to Cameron
user.setPassword("n0tte11ing");//password is initialized to n0tte11ing

I think even the uninitiated Java programmer would recognize that we have just created a user named Cameron with a password of n0tte11ing. We see this type of object creation and property initialization in Java programs all the time. But the problem Java developers always have is figuring out how to take the data associated with the object and save it to the database. Hibernate makes the persistence of your Java objects, aka Java Persistence, easy.
Just how easy is it to persist Java objects with Hibernate?

With a magical and mystical object known as the Hibernate Session, persisting the state of your Java objects is easy. Look how readable and understandable the following code is:

User user = new User(); //an object named user is created
user.setName("Cameron"); //name is initialized to Cameron
user.setPassword("n0tte11ing");//password is initialized to n0tte11ing
Session hibernateSession = HibernateUtil.getSession();
//get the magical Hibernate session
hibernateSession.save(user); //save the user to the database!

The line of code hibernateSession.save(user); saves the state of the user instance to the database. Of course, there's a little bit of plumbing code that needs to go in there to make the whole hibernate framework work; But setting up that plumbing code really isn't that bad. Overall, Hibernate is real easy to use, fairly easy to set up, and probably the easiest way to manage the persistent state of you domain model objects.

What are JPA Annotations?
Explaining why JPA annotations are better than crummy hbm mapping files...

JPA annotations greatly simplify persistence programming with Hibernate, but to understand why they're so great, it helps to understand what we needed to do before the introduction of annotations.
Back to the Future: This Historical hibernate-mapping.xml File

Hibernate makes persisting the state of your Java objects incredibly simple. However, in order for Hibernate to know where to story your JavaBeans, or how to map the property of a JavaBean to a database column, the developer has to provide a bit of direction to the Hibernate framework. As such, people developing Hibernate based applications had to maintain an unweildly, monolithic mapping file that described how to save a given Java object to the database.

So, for example, if you had a class named Event that had three properties, one called id, one called birthday, and another property called title, you would have to add the following segment to a hibernate-mapping file:

<hibernate-mapping>

<class name="events.Event" table="EVENTS">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="birthday" type="timestamp"/>
<property name="title"/>
</class>

</hibernate-mapping>

What's wrong with an XML mapping file?

There is nothing inherently wrong, with a mapping file, and in fact, thousands of very salacious hibernate applications that are in production use an XML mappings file, but having a big XML mapping file presents a variety of non-lethal, but certainly annoying problems, including the following:

* information about the Java class must be maintained in an external file
* XML isn't always easy to write
* with lots of classes, the XML file can become unweildly and massive
* errors in one part of the XML file can ricochet all over your Java program

Anyways, Java 5 introducted a new Java based artifact - that annotation. Basically, an annotation allows you to add detail an information about a Java class, without damaging, disturbing or changing any of the code that is actually found inside a Java class or a Java method. So, instead of using a monolithic mappings file, Hibernate with JPA annotations allows you to completely rid applications of a mapping file, and instead, you can annotate your Java classes like so:

@Entity
public class Event {
private Long id;
private String title;
private Date date;
@Id
@GeneratedValue
public Long getId() { return id; }
private void setId(Long id) {this.id = id;}
public Date getDate() {return date;}
public void setDate(Date date) {this.date = date;}
public String getTitle() {return title;}
public void setTitle(String title) {this.title = title;}
}

The @Entity, @Id and @GeneratedValue tags you see in the Event class are the JPA annotations, and they replace the neeed to describe how to persist your Java classes in an external hibernate-mappings.xml file. Instead of using an external file, each Java class maintains its own mapping information, which is much more natural, and much easier to maintain on a class by class basis. Furthermore, it makes introducing new classes, or even removing persistent classes from your domain model, much much easier.

If you're using Hibernate, and you have the ability to choose between using annotations or using a hibernate-mapping file, well, it's really not much of a choice. Always use JPA annotations if you have a choice. JPA annotations are much easier to use, easy to maintain, and will help to make your Hibernate development experience a real pleasure. :)


A Quick Discussion: What is Hibernate? and How Does it Work?

_________________
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: Fri Jul 25, 2008 4:33 am 
Newbie

Joined: Wed Jul 23, 2008 2:50 am
Posts: 16
Cameron McKenzie wrote:
Wow....Easy Tiger...

That's alot of questions in there. Good questions, but sometimes one per post can make it easier to answer.

..............................................
A Quick Discussion: What is Hibernate? and How Does it Work?



Tanks a lot for your reply.
I have experiment just very litle and i could find some benefit of it.
As my experiences you are not able to have "some" complex query.
As i wrote in my question, i couldn't find any way to generate the
select t1.c1, t1.c2 from table1 as t1, table2 as t2 where t1.c1=t2.c2 and t2="something"
and i asked one of my friend that he worked more than one year with Hibernate. and he couldn't help me either. however i don't think it was to complex query.
I will be very glad if you or any body can help me with this query.
i could generate to this statement: select t1.c1 t1.c2 from table1 as t1 , table2 as t2 where t1.c1=t2.c2 but not further...

Thanks in forehand for any help.....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 4:42 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Quote:
select t1.c1, t1.c2 from table1 as t1, table2 as t2 where t1.c1=t2.c2 and t2="something"


A criteria query could do it, using a Like MatchMode. It all depends on what you mean by "something."

On my site I have a tutorial on using the Hibernate Criteria API. Just follow my signature links!

_________________
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: Fri Jul 25, 2008 4:49 am 
Newbie

Joined: Wed Jul 23, 2008 2:50 am
Posts: 16
Cameron McKenzie wrote:
Quote:
select t1.c1, t1.c2 from table1 as t1, table2 as t2 where t1.c1=t2.c1 and t2.c2="something"


A criteria query could do it, using a Like MatchMode. It all depends on what you mean by "something."

On my site I have a tutorial on using the Hibernate Criteria API. Just follow my signature links!


Thanks again, Cameron!

...table2 as t2 where t1.c1=t2.c1 and t2.c2="something"
something is just a string which i can find in the t2.c2
and i will under time look at the your tutorial. and i find you have very nice describe the hibernate on your site....

R. Lars


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 9:55 am 
Newbie

Joined: Wed Jul 23, 2008 2:50 am
Posts: 16
Cameron McKenzie wrote:
Quote:
select t1.c1, t1.c2 from table1 as t1, table2 as t2 where t1.c1=t2.c2 and t2="something"


A criteria query could do it, using a Like MatchMode. It all depends on what you mean by "something."

On my site I have a tutorial on using the Hibernate Criteria API. Just follow my signature links!


camreon!
I've read all your tutorial, but i couldn't find any joining example on your pages, have you any concrete example of joining?

I would be very happy to know how i can use joining with criteria API or generally with Hibernate.

Thanks


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