John Cooper
John Cooper

Reputation: 7631

Couple of questions in hibernate ORM.

<hibernate mapping package="org.hibernate.tutorial.domain">

    <class name="Event" table"Events">
       <id name="id" column="EVENT_ID">
          <generator class ="native"/>
       </id>

       <property name="date" type="timestamp" column="EVENT_DATE"/>
       <property name="title"/>

       </class>

</hibernate-mapping>

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/tutorial.html#tutorial-firstapp I was going through this article on hibernate.

  1. Is this the best guide for starters.
  2. What does generator class ="native" mean and what does it do?
  3. why is the id declared with id tag, but the other declared with property tag.
  4. do people still use xml file for mapping class with the table. Is it an old fashioned way.

Upvotes: 1

Views: 112

Answers (3)

RP-
RP-

Reputation: 5837

Answers to your questions:

1)Is this the best guide for starters. - It is an official and first ever documentation for hibernate. I rather prefer "Hibernate in Action"

2) There are several generators in hibernate, depends on requirement we need to choose the proper one, Basically it is used to generate the primary key. For example if you use generator="assigned" i.e., you need to manually assign a primary key before you call session.save(entity). Here native means hibernate will take care of generating primary key based on the database dialect you provided in configuration. It uses sequence if you use oracle and auto_increment if you use mysql or postgres

3) id denotes the primary key, others are properties, Hope this is for a convention.

4) Older applications still uses xmls. It is better to start with xmls while learning then convert them to annotations.

Upvotes: 1

Joeri Hendrickx
Joeri Hendrickx

Reputation: 17435

  1. That's a very subjective answer. I would start from a JPA guide myself.
  2. Native means it will use the generation method that is specific to the database. For instance, for mysql it will use auto-increment. For oracle it could use sequences.
  3. Hibernate requires each entity to have an id. Use the id element to mark it.
  4. Most people now use annotations instead. There's the advantage there that you can use JPA annotations and not be tied to hibernate. Still there is a place in the world for XML config, for instance, to map beans you don't have source access to.

Upvotes: 2

Dave Newton
Dave Newton

Reputation: 160191

  1. Probably.
  2. It means to use the database's "native" id methodology, like a sequence in Oracle or an auto_increment in MySql, etc.
  3. Because one's an ID and the others are properties.
  4. Yes. Yes, but it's also preferable in some situations, e.g., wanting/needing to decouple the source from Hibernate or keeping the DB config in one place (XML files in a single directory instead of in source files wherever they may be).

Upvotes: 2

Related Questions