Showing posts with label Primary Key. Show all posts
Showing posts with label Primary Key. Show all posts

Saturday, December 31, 2011

One table for 2 or More Classes

In some situations we may need create 1 table out of 2 or more classes. for instance when we map inheritance.

New Annotations :
  • @Embeddable : when a class is going to be embedded inside another persistence class we have to annotate the class as embeddable
  • @Embedded : When some (object) field is going to persist inside a class. The object type must annotated as embeddable

Genetating Primary Key (HIBERNATE Note 2)

Here I am going to note down how to generate primary keys in hibernate.
Q : How do I drop a schema?

AnnotationConfiguration config=new AnnotationConfiguration();
config.addAnnotatedClass(Student.class);
config.configure("hibernate.cfg.xml");
new SchemaExport(config).drop(true, true);

ok lets see how to annotate a class to generate primary keys.

Note : Useful annotations

  • @Table (name="TableName") : Specify a name for the table
  • @Column(name="ColumnName") : Specify a column name
  • @Column(nullable=false) : the specified column cannot be empty
  • @Transient : The relevant attribute is volatile. not persistent
  • @Basic : default annotation. if we don't give annotation. default one become Basic
  • @Temporal(TemporalType.DATE) :  used to annotate date types
  • @Temporal(TemporalType.TIME) : used to annotate time
  • @Temporal(TemporalType.TIMESTAMP) : used to annotate date and time (time stamp)
  • @GeneratedValue : generate values automatically
  • @GeneratedValue(strategy=GenerationType.TABLE, generator="stdId") most DBs support this type. and generate keys in a sequence. SEQUENCE type is not supported by DBDerby.

© kani.stack.notez 2012 | Blogger Template by Enny Law - Ngetik Dot Com - Nulis