Showing posts with label Mysql. Show all posts
Showing posts with label Mysql. Show all posts

Saturday, January 12, 2013

Data Access With Spring JDBC (Note 1)

When developing enterprise applications, we have lots of options when it comes to the implementation of the DAO (Data access Object) layer. Either we may use an ORM framework like Hibernate, iBatis or pure JDBC. 

JDBC fulfills almost all the necessary data access requirements. and provides low level access to the data access operations. But we have to handle connections and lots of other things. 

Monday, January 2, 2012

Inheritance Mapping

Basically there are 3 types of inheritance mapping. We may use those methods suitably.
  • SINGLE_TABLE (default)
  • JOINED
  • TABLE_PER_CLASS
Model

Sunday, January 1, 2012

Compound Primary Keys in HIBERNATE

Most of the time we need only 1 primary key. But there are some situations we may need compound primary keys.
Here in hibernate it is simple. We can use 2 classes into 1 table strategy.
Account.java
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Account {

 private AccountCompoundKey compoundKey;
 private int accBalance;
 
 @Id
 public AccountCompoundKey getCompoundKey() {
  return compoundKey;
 }
 public void setCompoundKey(AccountCompoundKey compoundKey) {
  this.compoundKey = compoundKey;
 }
 public int getAccBalance() {
  return accBalance;
 }
 public void setAccBalance(int accBalance) {
  this.accBalance = accBalance;
 } 
}

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.

Getting started with HIBERNATE note 1

The best Hibernate video tutorial I have ever seen.
http://www.youtube.com/user/patrickwashingtondc
I have configured the user library and hibernate.cfg.xml for the MySQL.
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernatesample</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <!-- define default schema -->
        <property name="hibernate.default_schema">hibernatesample</property>
© kani.stack.notez 2012 | Blogger Template by Enny Law - Ngetik Dot Com - Nulis