Source Code of FirstHibernateProgram.java
package com.t4b.demo.bookshop;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class FirstHibernateProgram {
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
try {
Publisher publisher = new Publisher();
publisher.setId(10l);
publisher.setCode("X001");
publisher.setName("XXX University Press");
publisher.setAddress("Address 1");
session.save(publisher);
publisher.setAddress("Address 2");
session.update(publisher);
session.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
}
Source Code of Publisher.java
package com.t4b.demo.bookshop;
public class Publisher {
private Long id;
private String code;
private String name;
private String address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Source Code of Publisher.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.t4b.demo.bookshop">
<class name="Publisher" table="PUB">
<id name="id" type="long" column="ID">
<generator class="native" />
</id>
<property name="code" type="string">
<column name="CODE" length="4" not-null="true" unique="true" />
</property>
<property name="name" type="string">
<column name="PUBLISHER_NAME" length="100" not-null="true" />
</property>
<property name="address" type="string">
<column name="ADDRESS" length="200" />
</property>
</class>
</hibernate-mapping>
Source Code of hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/hb_db
</property>
<property name="connection.username">root</property>
<property name="connection.password">YOUR PASSWORD</property>
<property name="show_sql">true</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="connection.autocommit">true</property>
<!-- Mapping files -->
<mapping resource="com/t4b/demo/bookshop/Publisher.hbm.xml" />
</session-factory>
</hibernate-configuration>
Source Code of log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n
### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n
log4j.rootLogger=error, stdout
log4j.logger.org.hibernate.SQL=debug
log4j.logger.org.hibernate.type=debug
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.