Hi All,
The code is as follows :
code:
try {
System.out.println("Hi....");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
System.out.println("After Hi....");
session = sessionFactory.openSession();
System.out.println("Inserting Record");
Transaction tx = session.beginTransaction();
Contact contact = new Contact();
// contact.setId(7);
contact.setFirstName("Neel");
contact.setLastName("Bhargava");
contact.setEmail("
[email protected]");
Address address = new Address();
address.setAddress1("Flat No. 59");
address.setAddress2("Rajouri Apartments");
address.setCity("New Delhi");
address.setZipcode(110064);
contact.setHomeAddress(address);
session.save(contact);
tx.commit();
System.out.println("Done");
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
session.flush();
session.close();
}
hibernate.cfg.xml
code:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.datasource">java:MySqlDS</property>
<property name="hibernate.jndi.url"></property>
<property name="hibernate.jndi.class"></property>
<property name="hibernate.connection.username"></property>
<property name="hibernate.connection.password"></property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- Show and print nice SQL on stdout. -->
<!-- <property name="dialect">org.hibernate.dialect.MySQLDialect</property> -->
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- List of XML Mapping files -->
<mapping resource="contact.hbm.xml"/>
<!-- <mapping resource="address.hbm.xml"/> -->
</session-factory>
</hibernate-configuration>
Exception which occurs is :
code:
Hi....
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Could not find datasource
Exception in thread "main" java.lang.NullPointerException
at com.example.jndi.SaveExample.main(SaveExample.java:50)
Its gives exception in session.flush(); statement.
mysql-ds.xml contents
code:
<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/jbossdb</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>root</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<!-- should only be used on drivers after 3.22.1 with "ping" support
<valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
-->
<!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->
<!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
mysql-ds.xml is in deploy directory of JBoss. and JBoss shows
quote:
09:09:43,661 INFO [ConnectionFactoryBindingService] Bound ConnectionManager 'jboss.jca:service=DataSourceBinding,name=MySqlDS' to JNDI name 'java:MySqlDS'
Please help. !!! Any help will be greatly appreciated.
Thanks in advance.