我有休眠 4.1.5.SP1 和 PostreSQL 9.1。
这是日志:
INFO: HHH000397: Using ASTQueryTranslatorFactory
Initial SessionFactory creation failed.org.hibernate.MappingException: Could not instantiate persister org.hibernate.persister.entity.SingleTableEntityPersister
Exception in thread "main" java.lang.ExceptionInInitializerError
at util.HibernateUtil.<clinit>(HibernateUtil.java:22)
at Dao.VideoDao.getAllVideos(VideoDao.java:96)
at Dao.Main.main(Main.java:17)
Caused by: org.hibernate.MappingException: Could not instantiate persister org.hibernate.persister.entity.SingleTableEntityPersister
at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:174)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:135)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:381)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
at util.HibernateUtil.<clinit>(HibernateUtil.java:18)
这是hibernate.cfg.xml
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/mybase</property>
<property name="connection.username">postgres</property>
<property name="connection.password">******</property>
<property name="connection.pool_size">1</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<mapping resource="Video.hbm.xml"/>
这是 Video.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="logic.Video" table="video">
<id column="id" name="id" type="java.lang.Long">
<generator class="increment"/>
</id>
<property column="description" name="description" type="java.lang.String"/>
</class>
</hibernate-mapping>
这是 HibernateUtil.java
package util;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.SessionFactory;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
static {
try {
Configuration configuration = new Configuration();
configuration.configure();//("D:\\java\\workspace\\hibernate3\\bin\\hibernate.cfg.xml");
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
这是 Video.java
package Logic;
public class Video {
private Long id;
private String description;
public Video(){}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
这是 IVideoDao.java
package Dao;
import Logic.Video;
import java.util.Collection;
import java.sql.SQLException;
public interface IVideoDao {
public void addVideo(Video video) throws SQLException;
public void updateVideo(Long id, Video video) throws SQLException;
public Video getVideoById(Long id) throws SQLException;
public Collection<Video> getAllVideos() throws SQLException;
public void deleteVideo(Video video) throws SQLException;
public Collection<Video> getParamVideos(Long start, Long finish) throws SQLException;
}
这是 VideoDao.java
package Dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.Query;
import org.hibernate.Session;
import util.HibernateUtil;
import Logic.Video;
public class VideoDao implements IVideoDao
{
@Override
public void addVideo(Video video) throws SQLException
{
Session session = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(video);
session.getTransaction().commit();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage(), "Error 'addVideo'", JOptionPane.OK_OPTION);
}
finally
{
if (session != null && session.isOpen())
{
session.close();
}
}
}
@Override
public void updateVideo(Long id, Video video) throws SQLException
{
Session session = null;
try
{
session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.update(video);
session.getTransaction().commit();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage(), "Error 'updateVideo'", JOptionPane.OK_OPTION);
} finally {
if (session != null && session.isOpen())
{
session.close();
}
}
}
@Override
public Video getVideoById(Long id) throws SQLException
{
Session session = null;
Video video = null;
try
{
session = HibernateUtil.getSessionFactory().openSession();
video = (Video) session.load(Video.class, id);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage(), "Error 'findById'", JOptionPane.OK_OPTION);
}
finally
{
if (session != null && session.isOpen())
{
session.close();
}
}
return video;
}
@Override
public Collection<Video> getAllVideos() throws SQLException
{
Session session = null;
List<Video> videos = new ArrayList<Video>();
try
{
session = HibernateUtil.getSessionFactory().openSession();
videos = session.createCriteria(Video.class).list();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage(), "Error 'getAll'", JOptionPane.OK_OPTION);
}
finally
{
if (session != null && session.isOpen())
{
session.close();
}
}
return videos;
}
@Override
public void deleteVideo(Video video) throws SQLException
{
Session session = null;
try
{
session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.delete(video);
session.getTransaction().commit();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage(), "Error 'deleteVideo'", JOptionPane.OK_OPTION);
}
finally
{
if (session != null && session.isOpen())
{
session.close();
}
}
}
@Override
public Collection<Video> getParamVideos(Long start, Long finish) throws SQLException
{
Session session = null;
List<Video> videos = new ArrayList<Video>();
try {
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query = session.createQuery(
" select v "
+ " from video v "
+ " where id between :startId and :finishId "
)
.setLong("startId", start)
.setLong("finishId", finish);
videos = (List<Video>) query.list();
session.getTransaction().commit();
}
finally
{
if (session != null && session.isOpen())
{
session.close();
}
}
return videos;
}
}
这是Factory.java
package Dao;
public class Factory {
private static VideoDao videoDao = null;
private static Factory instance = null;
public static synchronized Factory getInstance()
{
if (instance == null)
{
instance = new Factory();
}
return instance;
}
private Factory(){}
public IVideoDao getVideoDao()
{
if (videoDao == null)
{
videoDao = new VideoDao();
}
return videoDao;
}
}
这是 Main.java
package Dao;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Iterator;
import Logic.Video;
public class Main {
/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
Collection<Video> videos = Factory.getInstance().getVideoDao().getAllVideos();
Iterator<Video> iterator = videos.iterator();
System.out.println("========All films=========");
while (iterator.hasNext()) {
Video video = iterator.next();
System.out.println(video.getId().toString() + ") Video: " + video.getDescription());
}
}
}