![]() |
| |||||||||||||||||||||||||||||
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:
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.
The example uses the same database table as the previous basic example.
Creating a new entity in Amber just means creating the bean with normal Java calls and then asking Amber to create the entity.
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 KeysIt'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:
The identity generator lets the database generate the key and returns the value using the JDBC API.
The AmberConnection delete call deletes the object from the database and makes the bean object transient, i.e unregistered from the AmberConnection.
When the delete commits, the bean is transient. It's no longer registered with Amber and it is deleted from the database.
| ||||||||||||||||||||||||||||||