I was just reading book from apress, Beginning Hibernate 2nd edition, i tried the hello world example using mysql database, but it throws an exception. Here is the code
im using netbeans anyway..
Code:
package sample.entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
@Entity
public class Message implements Serializable {
private String messageText;
private Integer id;
public Message(){}
public Message(String messageText) {
this.messageText = messageText;
}
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getMessageText() {
return messageText;
}
public void setMessageText(String messageText) {
this.messageText = messageText;
}
}
Code:
package sample;
import java.util.Date;
import sample.entity.Message;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class PopulateMessage {
public static void main(String[] args) {
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Message m1 = new Message("Hibernated message on "+new Date());
session.save(m1);
session.getTransaction().commit();
session.close();
}
}
and here is the configuration file
Code:
<?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 name="session1">
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernateSample</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
then this exception appear
Code:
Sep 9, 2010 9:45:23 AM org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.5.5-Final
Sep 9, 2010 9:45:23 AM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.5.5-Final
Sep 9, 2010 9:45:23 AM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Sep 9, 2010 9:45:23 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : javassist
Sep 9, 2010 9:45:23 AM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Sep 9, 2010 9:45:24 AM org.hibernate.annotations.common.Version <clinit>
INFO: Hibernate Commons Annotations 3.2.0.Final
Sep 9, 2010 9:45:24 AM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Sep 9, 2010 9:45:24 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Sep 9, 2010 9:45:24 AM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : Event.hbm.xml
Sep 9, 2010 9:45:24 AM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
Exception in thread "main" java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/validation/Validation
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:170)
at org.hibernate.cfg.beanvalidation.BeanValidationActivator.activateBeanValidation(BeanValidationActivator.java:34)
at org.hibernate.cfg.AnnotationConfiguration.enableBeanValidation(AnnotationConfiguration.java:1034)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:952)
at sample.PopulateMessage.main(PopulateMessage.java:10)
Java Result: 1
please any help would be appreciated...