|
Today, when the entity classes use hibernate annotation manner indicated generation database representation encountered some problems, after gathering some information, and finally summed up as follows:
The code I put a whole posted a few files come
This is the hibernate.cfg.xml file
< ? Xml version = '1.0' encoding = 'UTF-8'?>
< ! DOCTYPE hibernate-configuration PUBLIC
"- // Hibernate / Hibernate Configuration DTD 3.0 // EN"
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
< -! Generated by MyEclipse Hibernate Tools -.>
< Hibernate-configuration>
< Session-factory>
< Property name = "hibernate.connection.driver_class"> com.mysql.jdbc.Driver < / property>
< Property name = "hibernate.connection.url"> jdbc: mysql: // localhost: 3306 / phn_dsjava < / property>
< Property name = "hibernate.connection.username"> root < / property>
< Property name = "hibernate.connection.password"> 123456 < / property>
< Property name = "hibernate.dialect"> org.hibernate.dialect.MySQLDialect < / property>
< Property name = "hibernate.show_sql"> true < / property>
< Property name = "format_sql"> true < / property>
< Property name = "hibernate.hbm2ddl.auto"> update < / property>
< Mapping class = "com.phn.bean.Announces" />
< / Session-factory>
< / Hibernate-configuration>
This is the entity class Announces.java
package com.phn.bean;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/ **
*
* /
@Entity
@Table (Name = "t_announce")
public class Announces {
private Integer id;
private String announcement;
private String title;
private Date thetime;
@Id
@GeneratedValue
@Column (Nullable = false)
public Integer getId () {
return this.id;
}
public void setId (Integer id) {
this.id = id;
}
@Column (Length = 20000)
public String getAnnouncement () {
return this.announcement;
}
public void setAnnouncement (String announcement) {
this.announcement = announcement;
}
@Column (Length = 100)
public String getTitle () {
return title;
}
public void setTitle (String title) {
this.title = title;
}
public Date getThetime () {
return thetime;
}
public void setThetime (Date thetime) {
this.thetime = thetime;
}
}
This is a test class HibernateAnnotationTest.java
package com.phn.junitTest;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.Test;
import junit.framework.TestCase;
public class HibernateAnnotationTest extends TestCase {
@Test
public void testSQL () {
AnnotationConfiguration configuration = new AnnotationConfiguration ();
configuration.configure ();
SessionFactory sessionFactory = configuration.buildSessionFactory ();
}
}
--------------------------- Below is a problem to explain ------------------ ---------
Hibernate configuration file hibernate.cfg.xml located in the src directory. When unit testing, executing the following code will generate an exception.
Configuration cfg = new Configuration ();
SessionFactory sf = cfg.configure () buildSessionFactory ().;
abnormal:
org.hibernate.MappingException: An AnnotationConfiguration instance is required to use < mapping class = "***" />
Hibernate configuration file, if with < mapping class = "com.phn.Users" />, then the mapping class, using the Annotation mode. At initialization Configuation, use AnnoationConfiguration, the following code:
AnnotationConfiguration configuration = new AnnotationConfiguration ();
configuration.configure ();
SessionFactory sessionFactory = configuration.buildSessionFactory ();
Note: As used herein, hibernate version 3.3
Version low point may appear in the picture below this error, because the low version of hibernate jar package org.hibernate.engine.query.sql.NativeSQLQueryReturn class is missing, and therefore it is updated on the line |
|
|
|