Creating and Deleting Persistent Beans
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
Basic Amber
Tutorials
Find/Query

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

Creating and deleting beans from the database uses AmberConnection's create() and delete() methods.

This example explains:

  • Using the create() method to add a bean to the database
  • Using the delete() method to remove a bean from the database

  1. Files in this tutorial
  2. Database Schema
  3. Creating a Course
    1. Generated Keys
  4. Deleting a Course

Almost all applications need to add and remove entities from the database. Although most database accesses are reads, eventually we need to change the database.

Files in this tutorial

WEB-INF/web.xml Configures the Amber resource and Database
WEB-INF/classes/example/Course.java The Course bean.
WEB-INF/classes/example/Course.hbm.xml The Course configuration file.
WEB-INF/classes/example/CreateServlet.java The Create servlet.
WEB-INF/classes/example/InitResource.java A resource to initialize the database for the demo

Database Schema

The example uses the same database table as the previous basic example.

create.sql
CREATE TABLE amber_create_courses (
  id INTEGER PRIMARY KEY auto_increment,

  course VARCHAR(250) NOT NULL,
  instructor VARCHAR(250),
);

Creating a Course

Creating a new entity in Amber just means creating the bean with normal Java calls and then asking Amber to create the entity.

Creating a Course
AmberConnection aConn = factory.getConnection();

Course divination = new Course();
divination.setName("Divination");
divination.setTeacher("Sybil Trelawney");

aConn.create(divination);

Amber only inserts the new course in the database at the create call. Before the create, the Course is a transient object, unconnected with Amber or the database.

After the create, the object is persistent: registered with Amber and existing in the database. In other words, it's live.

Generated Keys

It's convenient to use integers or longs as table keys and let the database generate the keys itself, avoiding potential conflicts.

In the example, the database will generate the id and Amber will call the setId method to fill the new key. After the create call, getId() will return the actual key generated by the database.

Key generation is configured in the hbm.xml file by adding a <generate> tag:

Course.hbm.xml
<hibernate-mapping>
  <class name="example.Course" table="amber_create_courses">
    <id name="id">
      <generator class="identity"/>
    </id>

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

The identity generator lets the database generate the key and returns the value using the JDBC API.

Deleting a Course

The AmberConnection delete call deletes the object from the database and makes the bean object transient, i.e unregistered from the AmberConnection.

Delete a Course
AmberConnection aConn = factory.getConnection();

aConn.delete(divination);

When the delete commits, the bean is transient. It's no longer registered with Amber and it is deleted from the database.

Try the Tutorial


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