[ start | index | login ]

Mapping

Created by mpecher. Last edited by mpecher, 7 years and 287 days ago. Viewed 7,895 times. #9
[diff] [history] [edit] [rdf]
labels
attachments

Minimum required/recommended:

  1. No args constructor
  2. id field (prefer Long)
  3. version field
  4. override hashcode() and equals()

one-to-one mapping

A one-to-one association to another persistent class is declared using a one-to-one element. >> More Info

many-to-one mapping

An ordinary association to another persistent class is declared using a many-to-one element. The relational model is a many-to-one association: a foreign key in one table is referencing the primary key column(s) of the target table. >> More Info

XDoclet Mapping

Good reference at >>XDoclet for Hibernate . >>Link to hibernate xdoclet properties

To use XDoclet to generate the hibernate mapping file the following minimum tags are required for the Class:

  1. Class Level: @hibernate.class table="customers"
  2. Any properties to be mapped, require the getter method to have annotation
    1. Getter method for Id Property: @hibernate.id generator-class="native"
    2. Getter method for properties: @hibernate.property
eg:
package com.marandcustomsolutions.model;

import java.io.Serializable;

/** * Title: Account * Description: Holds the information about a Client (company) * Copyright: Copyright (c) 2001 * Company: Marand Custom Solutions * * @author Marcus Pecher * @version 1.0 * * @hibernate.class table="account" */ public final class Account implements Serializable {

private static final long serialVersionUID = 1L;

private Address address = new Address();

private User contact;

private String name;

private String description;

private long id;

private boolean active;

private Integer version;

private String lastModifiedBy;

public Account() { }

/** * @hibernate.id column="id" generator-class="native" unsaved-value="null" */ public long getId() { return id; }

/** * @hibernate.version */ public Integer getVersion() { return version; }

/** * @hibernate.component */ public Address getAddress() { return address; }

/** * @hibernate.component */ public User getContact() { return contact; }

/** * @hibernate.property */ public String getName() { return name; }

/** * @hibernate.property */ public String getDescription() { return description; }

/** * @hibernate.property column="active" type="yes_no" */ public boolean isActive() { return active; }

public void setId(long newId) { id = newId; }

public void setAddress(Address newAddress) { address = newAddress; }

public void setContact(User newContact) { contact = newContact; }

public void setName(String newName) { name = newName; }

public void setDescription(String newDescription) { description = newDescription; }

public void setActive(boolean newActive) { active = newActive; }

public void setVersion(Integer version) { this.version = version; } }

Attributes that can be declared to map a Class

You may declare a persistent class using the class element:
<class
        name="ClassName"                              (1)
        table="tableName"                             (2)
        discriminator-value="discriminator_value"     (3)
        mutable="true|false"                          (4)
        schema="owner"                                (5)
        catalog="catalog"                             (6)
        proxy="ProxyInterface"                        (7)
        dynamic-update="true|false"                   (8)
        dynamic-insert="true|false"                   (9)
        select-before-update="true|false"             (10)
        polymorphism="implicit|explicit"              (11)
        where="arbitrary sql where condition"         (12)
        persister="PersisterClass"                    (13)
        batch-size="N"                                (14)
        optimistic-lock="none|version|dirty|all"      (15)
        lazy="true|false"                             (16)
        entity-name="EntityName"                      (17)
        check="arbitrary sql check condition"         (18)
        rowid="rowid"                                 (19)
        subselect="SQL expression"                    (20)
        abstract="true|false"                         (21)
        node="element-name"
/>
  1. name (optional): The fully qualified Java class name of the persistent class (or interface). If this attribute is missing, it is assumed that the mapping is for a non-POJO entity.
  2. table (optional - defaults to the unqualified class name): The name of its database table.
  3. discriminator-value (optional - defaults to the class name): A value that distiguishes individual subclasses, used for polymorphic behaviour. Acceptable values include null and not null.
  4. mutable (optional, defaults to true): Specifies that instances of the class are (not) mutable.
  5. schema (optional): Override the schema name specified by the root <hibernate-mapping> element.
  6. catalog (optional): Override the catalog name specified by the root <hibernate-mapping> element.
  7. proxy (optional): Specifies an interface to use for lazy initializing proxies. You may specify the name of the class itself.
  8. dynamic-update (optional, defaults to false): Specifies that UPDATE SQL should be generated at runtime and contain only those columns whose values have changed.
  9. dynamic-insert (optional, defaults to false): Specifies that INSERT SQL should be generated at runtime and contain only the columns whose values are not null.
  10. select-before-update (optional, defaults to false): Specifies that Hibernate should never perform an SQL UPDATE unless it is certain that an object is actually modified. In certain cases (actually, only when a transient object has been associated with a new session using update()), this means that Hibernate will perform an extra SQL SELECT to determine if an UPDATE is actually required.
  11. polymorphism (optional, defaults to implicit): Determines whether implicit or explicit query polymorphism is used.
  12. where (optional) specify an arbitrary SQL WHERE condition to be used when retrieving objects of this class
  13. persister (optional): Specifies a custom ClassPersister.
  14. batch-size (optional, defaults to 1) specify a "batch size" for fetching instances of this class by identifier.
  15. optimistic-lock (optional, defaults to version): Determines the optimistic locking strategy.
  16. lazy (optional): Lazy fetching may be completely disabled by setting lazy="false".
  17. entity-name (optional, defaults to the class name): Hibernate3 allows a class to be mapped multiple times (to different tables, potentially), and allows entity mappings that are represented by Maps or XML at the Java level. In these cases, you should provide an explicit arbitrary name for the entity. See Section 4.4, ?Dynamic models? and Chapter 18, XML Mapping for more information.
  18. check (optional): A SQL expression used to generate a multi-row check constraint for automatic schema generation.
  19. rowid (optional): Hibernate can use so called ROWIDs on databases which support. E.g. on Oracle, Hibernate can use the rowid extra column for fast updates if you set this option to rowid. A ROWID is an implementation detail and represents the physical location of a stored tuple.
  20. subselect (optional): Maps an immutable and read-only entity to a database subselect. Useful if you want to have a view instead of a base table, but don't. See below for more information.
  21. abstract (optional): Used to mark abstract superclasses in <union-subclass> hierarchies.

no comments | post comment

Menu:
Java & J2EE
Development
Books

Help:
Help FAQ
Formatting


< April 2014 >
SunMonTueWedThuFriSat
12345
6789101112
13141516171819
20212223242526
27282930


Logged in Users: (1)
… and 45 Guests.



Disclaimer: Views and opinions are that of the individual author, and not that of Marand Custom Solutions. This site is an open forum for technical content, and the company accepts no liability for any content or view expressed.