Hibernate Misc

hibernate

// To prevent Hibernate from recreating the tables from scratch, change the
// hbm2ddl.auto property in hibernate.cfg.xml from "create" to "update".

@GeneratedValue // Tell Hibernate to automatically generate a value for our surrogate key
@GeneratedValue(strategy=GenerationType.AUTO)

@AttributeOverrides({  // Override the column name for embedded classes in cases that
                       // we are embedding multiple instances of the same class
  @AttributeOverride (name="street", column=@Column(name="HOME_STREET_NAME")),
  @AttributeOverride (name="city", column=@Column(name="HOME_CITY_NAME"))
})

@GenericGenerator(name="hilo-gen", strategy="hilo")

// Generate the schema from the mappings:
DDL can be generated from mapping files by using SchemaExport tool that Hibernate 
provides. We need to make sure that the mapping files provide detailed constraints 
like not null, maximum length, unique keys, foreign keys so that the schema generated 
has appropriate relationships and constraints. Command used to run SchemaExport:
java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaExport options mapping_files
SchemaExport has the feature to generate incremental updates as well. Be careful 
when you use it because it depends on JDBC metadata API.

// SchemaValidator:
The SchemaValidator tool can be used to verify if the mapping configured matches 
the existing database structure.

// Hibernate bahviors:
If we make changes to the object after saving it but before committing the transaction
Hibernate will detect the changes and generate one single update statement for all the 
changes and execute that statement to persist the changes to the database.

@Embeddable
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License