By using access field , there is no need to create getter/setter methods in the POJO file.
There are three values for access attribute
(a) field
(b) property (default)
(c) ClassName
However this attribute is optional but its default value is property.
It can be specified at any of the following point
(a) Hibernate-mapping node of hbm file
or
(b) in each id and property node of class node in the hbm file.
But if we dont create getter/setter methods for attibute and we have also not given access attribute in hbm file and we have also mentioned its mapping in hbm file then hibernate will throw exception
org.hibernate.PropertyNotFoundException: Could not find a getter for firstName in class accesstest.Person
1. Create POJO File
package accesstest;
public class Person {
private int id;
String firstName;
String lastName;
/*
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
*/
}
2. Create any of the following hbm files by name "Person.hbm.xml".
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate
Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Generated Dec 21, 2012 7:12:56 PM by Hibernate Tools 3.4.0.CR1
-->
<hibernate-mapping>
<class name="accesstest.Person"
table="Person" >
<id name="id"
type="int" access="field">
<column name="ID"
/>
<generator class="increment"
/>
</id>
<property name="firstName"
type="java.lang.String" access="field">
<column name="FIRSTNAME"
/>
</property>
<property name="lastName"
type="java.lang.String" access="field">
<column name="LASTNAME"
/>
</property>
</class>
</hibernate-mapping>
OR
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate
Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Generated Dec 21, 2012 7:12:56 PM by Hibernate Tools 3.4.0.CR1
-->
<hibernate-mapping default-access="field">
<class name="accesstest.Person"
table="Person">
<id name="id"
type="int" >
<column name="ID"
/>
<generator class="increment"
/>
</id>
<property name="firstName"
type="java.lang.String" >
<column name="FIRSTNAME"
/>
</property>
<property name="lastName"
type="java.lang.String" >
<column name="LASTNAME"
/>
</property>
</class>
</hibernate-mapping>
3. Create main method
package accesstest;
import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class Main {
public static void main(String args[]){
try
{
SessionFactory factory = new Configuration().configure("a.cfg.xml").buildSessionFactory();
Session session = factory.openSession();
Transaction t = session.beginTransaction();
Person p = new Person();
p.firstName = "PRASHANT 3";
p.lastName =" Lal 3";
//p.setFirstName("prashant");
//p.setLastName("lal");
session.saveOrUpdate(p);
session.flush();
t.commit();
session.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
4. Create hibernate configuration file
<?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/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool-size">10</property>
<property name="show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<mapping resource="accesstest/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5. Run the Main.java file
Hibernate Output
Dec 21, 2012
8:16:36 PM org.hibernate.cfg.Configuration addResource
INFO: Reading
mappings from resource : accesstest/P.hbm.xml
Dec 21, 2012
8:16:36 PM org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping
class: accesstest.P -> P
Dec 21, 2012
8:16:36 PM org.hibernate.cfg.Configuration doConfigure
INFO: Configured
SessionFactory: null
Dec 21, 2012
8:16:36 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using
Hibernate built-in connection pool (not for production use!)
Dec 21, 2012
8:16:36 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate
connection pool size: 20
Dec 21, 2012
8:16:36 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit
mode: false
Dec 21, 2012
8:16:36 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using
driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/test
Dec 21, 2012
8:16:36 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection
properties: {pool-size=10, user=root, password=****}
Dec 21, 2012
8:16:36 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: RDBMS:
MySQL, version: 5.5.22
Dec 21, 2012
8:16:36 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC driver:
MySQL-AB JDBC Driver, version: mysql-connector-java-3.0.15-ga ( $Date:
2004/08/09 22:15:11 $, $Revision: 1.27.2.43 $ )
Dec 21, 2012
8:16:36 PM org.hibernate.dialect.Dialect <init>
INFO: Using
dialect: org.hibernate.dialect.MySQLDialect
Dec 21, 2012
8:16:36 PM org.hibernate.transaction.TransactionFactoryFactory
buildTransactionFactory
INFO: Using
default transaction strategy (direct JDBC transactions)
Dec 21, 2012
8:16:36 PM org.hibernate.transaction.TransactionManagerLookupFactory
getTransactionManagerLookup
INFO: No
TransactionManagerLookup configured (in JTA environment, use of read-write or
transactional second-level cache is not recommended)
Dec 21, 2012
8:16:36 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic
flush during beforeCompletion(): disabled
Dec 21, 2012
8:16:36 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic
session close at end of transaction: disabled
Dec 21, 2012
8:16:36 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch
size: 15
Dec 21, 2012
8:16:36 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch
updates for versioned data: disabled
Dec 21, 2012
8:16:36 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable
result sets: enabled
Dec 21, 2012
8:16:36 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3
getGeneratedKeys(): enabled
Dec 21, 2012
8:16:36 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection
release mode: auto
Dec 21, 2012
8:16:36 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Maximum
outer join fetch depth: 2
Dec 21, 2012
8:16:36 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default
batch fetch size: 1
Dec 21, 2012
8:16:36 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL
with comments: disabled
Dec 21, 2012
8:16:36 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL
updates by primary key: disabled
Dec 21, 2012
8:16:36 PM org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query
translator: org.hibernate.hql.classic.ClassicQueryTranslatorFactory
Dec 21, 2012
8:16:37 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query
language substitutions: {}
Dec 21, 2012
8:16:37 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL
strict compliance: disabled
Dec 21, 2012
8:16:37 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level
cache: enabled
Dec 21, 2012
8:16:37 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache:
disabled
Dec 21, 2012
8:16:37 PM org.hibernate.cfg.SettingsFactory createCacheProvider
INFO: Cache
provider: org.hibernate.cache.NoCacheProvider
Dec 21, 2012
8:16:37 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize
cache for minimal puts: disabled
Dec 21, 2012
8:16:37 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured
second-level cache entries: disabled
Dec 21, 2012
8:16:37 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Echoing all
SQL to stdout
Dec 21, 2012
8:16:37 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics:
disabled
Dec 21, 2012
8:16:37 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted
entity synthetic identifier rollback: disabled
Dec 21, 2012
8:16:37 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default
entity-mode: pojo
Dec 21, 2012
8:16:37 PM org.hibernate.impl.SessionFactoryImpl <init>
INFO: building
session factory
Dec 21, 2012
8:16:37 PM org.hibernate.tuple.PojoInstantiator <init>
INFO: no default
(no-argument) constructor for class: inheritancesub.PersonEmployee (class must
be instantiated by Interceptor)
Dec 21, 2012
8:16:37 PM org.hibernate.tuple.PojoInstantiator <init>
INFO: no default
(no-argument) constructor for class: inheritancesub.Person (class must be
instantiated by Interceptor)
Dec 21, 2012
8:16:37 PM org.hibernate.tuple.PojoInstantiator <init>
INFO: no default
(no-argument) constructor for class: inheritancesub.Personowner (class must be
instantiated by Interceptor)
Dec 21, 2012
8:16:37 PM org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding
factory to JNDI, no JNDI name configured
Hibernate:
select max(ID) from P
Hibernate:
insert into P (FIRSTNAME, LASTNAME,
ID) values (?, ?, ?)
No comments:
Post a Comment