Amber Many-To-One Relations
Resin 3.0

Features
Installation
Configuration
Web Applications
JSP
Servlets and Filters
Portlets
Databases
Admin (JMX)
Amber
Security
XML and XSLT
XTP
Resources
Performance
Protocols
Third-party
Troubleshooting/FAQ

Amber
Lifecycle
Tutorials

Basic Amber
Create/Destroy
Find/Query
Many-To-One
One-To-Many
Find/Query
Tutorials
One-To-Many

Find this tutorial in: /opt/resin/webapps/resin-doc/amber/tutorial/many2one
Try the Tutorial

Introduces a many-to-one relation, i.e. a simple link.

  1. Files in this tutorial
  2. Database Schema
  3. Student Class
  4. Configuration
  5. Client Servlet

Amber manages tables in a relational database using a Java bean interface.

Files in this tutorial

WEB-INF/classes/example/Student.java The Student bean.
WEB-INF/classes/example/Student.hbm.xml The Student configuration file.
WEB-INF/classes/example/House.java The House bean.
WEB-INF/classes/example/House.hbm.xml The House configuration file.
WEB-INF/classes/example/ManyToOneServlet.java The ManyToOne test servlet.
WEB-INF/web.xml Configures the Amber resource and Database
WEB-INF/classes/example/InitResource.java A resource to initialize the database for the demo

Database Schema

course.sql
CREATE TABLE amber_many2one_houses (
  id INTEGER PRIMARY KEY,

  name VARCHAR(250),
);

INSERT INTO amber_many2one_houses VALUES (1, 'Gryffindor')
INSERT INTO amber_many2one_houses VALUES (2, 'Slytherin')
INSERT INTO amber_many2one_houses VALUES (3, 'Hufflepuff')
INSERT INTO amber_many2one_houses VALUES (4, 'Ravenclaw')
	
CREATE TABLE amber_many2one_students (
  id INTEGER PRIMARY KEY,
  name VARCHAR(250),
  house INTEGER
)

INSERT INTO amber_many2one_students VALUES (1, 'Harry Potter', 1)
INSERT INTO amber_many2one_students VALUES (2, 'Ron Weasley', 1)
INSERT INTO amber_many2one_students VALUES (3, 'Hermione Granger', 1)
	
INSERT INTO amber_many2one_students VALUES (4, 'Draco Malfoy', 2)
INSERT INTO amber_many2one_students VALUES (5, 'Millicent Bustrode', 2)
	
INSERT INTO amber_many2one_students VALUES (6, 'Justin Finch-Fletchly', 3)
INSERT INTO amber_many2one_students VALUES (7, 'Cho Chang', 3)
	
INSERT INTO amber_many2one_students VALUES (8, 'Terry Boot', 4)
INSERT INTO amber_many2one_students VALUES (9, 'Luna Lovegood', 4)

Student Class

Student.java
package example;

public class Course {
  private int _id;
  private String _name;
  private House _house;

  public int getId()
  {
    return _id;
  }

  public void setId(int id)
  {
    _id = id;
  }

  public String getName()
  {
    return _name;
  }

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

  public House getHouse()
  {
    return _house;
  }

  public void setHouse(House house)
  {
    _house = house;
  }
}

Configuration

example/Student.hbm.xml
<hibernate-mapping>
  <class name="example.Student" table="amber_many2one_students">
    <id name="id"/>

    <property name="name"/>

    <many-to-one name="house"/>
  </class>
</hibernate-mapping>

example/House.hbm.xml
<hibernate-mapping>
  <class name="example.House" table="amber_many2one_houses">
    <id name="id"/>

    <property name="name"/>
  </class>
</hibernate-mapping>

Hibernate configurationmeaningdefault
hibernate-mappingtop-level xml node for the Hibernate syntaxrequired
classConfigures a Java class as an entityrequired
namename of the class or propertyrequired
tablename of the database table for the classthe class name
idconfigures a field as the table's primary key
propertyconfigures a field as a property
many-to-oneconfigures a field as a many-to-one relationproperty

Client Servlet

Using the House from the Student
AmberConnection aConn = ...;

Student student = (Student) aConn.load(Student.class, 1);

out.println(student.getName() + ", " + student.getHouse().getName());

Using the house relation in a query
AmberConnection aConn = ...;

String sql = ("SELECT o.name, o.house.name" +
              " FROM example.Student AS o" +
              " ORDER BY o.house.name, o.name");

ResultSet rs = aConn.query(sql);
while (rs.next()) {
  out.print(rs.getString(1) + ", " + rs.getString(2));
}

Try the Tutorial


Find/Query
Tutorials
One-To-Many
Copyright © 1998-2003 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.