Required Libraries for Hibernate 4.2
- antlr-2.7.7.jar
- dom4j-1.6.1.jar
- hibernate-commons-annotations-4.0.1.Final.jar
- hibernate-core-4.2.1.Final.jar
- hibernate-jpa-2.0-api-1.0.1.Final.jar
- javassist-3.15.0-GA.jar
- jboss-logging-3.1.0.GA.jar
- jboss-transaction-api_1.1_spec-1.0.1.Final.jar
- postgresql-9.2-1002.jdbc3.jar *
* Database driver file.
just keep these annotations in your mind while using Hibernate in your application.
JPA Annotations
- Entity
- Id
- GeneratedValue
if you want that you java class may persist in the database with the same name you need to add @Entity annotation at the class level.
@Entity
public class Test{}
now it will persist into the database with a table name "test". if you want to change the name of the table then you need to add name attribute of Entity. like this @Entity(name = "TableName"). Entity class must implement serializable interface. Serializable interface acts as a mask for the object and objects retain their identity acorss the network.
Adding Columns into Table
adding columns is nothing difficult it is same you are defining data members in class. like this.
@Entity
public class Test implements Serializable{
int id;
String username;
String password;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
@Id annotation is to define a primary key of table and i have made it auto generated. now define an xml file to define this entity class and make common configuration like database name, username, password, host name.
hibernate.cfg.xml file code:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">umairbaig</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/imageDB</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<mapping class="Test"/>
</session-factory>
</hibernate-configuration>
When you are done configuration inside main method add these lines:
public static void main(String[] args) {
AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration.configure();
// if your xml file name is hibernate.cfg.xml then you dont need //to pass argument inside configure();
new SchemaExport(configuration).create(true, true);
}
Database table is created with the following columns;
Table Name test
Columns:
- id (Primary Key)
- username
- password
No comments:
Post a Comment