hallo,
folgende ausgangssituation:
- benutze myeclipse, mysql-db
- kardinalität der zu mappenden tabellen: m zu n
- also insgesamt 3 tabellen(je eine für die beiden entitäten und eine für die foreign keys)
ich habe jetzt ein kleines test-programm geschrieben, welches entsprechend datensätze in die tabellen schreiben soll.
HibernateSessionFactory
HibernateTest
jedoch bekomme ich folgende exceptions:
könnt ihr mir helfen?
folgende ausgangssituation:
- benutze myeclipse, mysql-db
- kardinalität der zu mappenden tabellen: m zu n
- also insgesamt 3 tabellen(je eine für die beiden entitäten und eine für die foreign keys)
ich habe jetzt ein kleines test-programm geschrieben, welches entsprechend datensätze in die tabellen schreiben soll.
HibernateSessionFactory
Code:
mport org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html}.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package - the default location
* is the default Java package.<br><br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
private static String CONFIG_FILE_LOCATION = "/hibernate/hibernate.cfg.xml";
/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();
/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();
/** The single instance of hibernate SessionFactory */
private static org.hibernate.SessionFactory sessionFactory;
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* Default constructor.
*/
private HibernateSessionFactory() {
}
}
HibernateTest
Code:
mport org.hibernate.*;
import java.util.*;
public class HibernateTest
{
public static void main(String[] args)
{
// Step 1 - Create a new entity
//User user = new User();
// Step 2 - Set message field
//user.setName("6th_user");
//user.setLogin("login6");
//user.setPassword("passwd6");
//RoleUser r_u = new RoleUser();
//r_u.setRole();
//r_u.setUser(user);
//Set r_u_set = new HashSet();
//r_u_set.add(user);
try
{
// Step 3 - Get a Hibernate Session
Session session = HibernateSessionFactory.currentSession();
// Step 4 - Persist entity to database
Transaction tx = session.beginTransaction();
//session.save(user);
tx.commit();
System.out.println("Save successful.");
}
catch (HibernateException e)
{
System.out.println("Save failed.");
}
finally
{
try
{
// Step 5 - close the session
HibernateSessionFactory.closeSession();
}
catch (HibernateException e1)
{
// do nothing
}
}
}
}
jedoch bekomme ich folgende exceptions:
Code:
(cfg.Environment 464 ) Hibernate 3.0.5
(cfg.Environment 477 ) hibernate.properties not found
(cfg.Environment 510 ) using CGLIB reflection optimizer
(cfg.Environment 540 ) using JDK 1.4 java.sql.Timestamp handling
(cfg.Configuration 1110) configuring from resource: /hibernate/hibernate.cfg.xml
(cfg.Configuration 1081) Configuration resource: /hibernate/hibernate.cfg.xml
(cfg.Configuration 444 ) Mapping resource: hibernate/RoleUser.hbm.xml
(cfg.HbmBinder 260 ) Mapping class: hibernate.RoleUser -> role_user
(cfg.Configuration 444 ) Mapping resource: hibernate/Role.hbm.xml
(cfg.HbmBinder 260 ) Mapping class: hibernate.Role -> role
(cfg.Configuration 444 ) Mapping resource: hibernate/User.hbm.xml
(cfg.HbmBinder 260 ) Mapping class: hibernate.User -> user
(cfg.Configuration 1222) Configured SessionFactory: null
(cfg.Configuration 875 ) processing extends queue
(cfg.Configuration 879 ) processing collection mappings
%%%% Error Creating SessionFactory %%%%
org.hibernate.MappingException: Association references unmapped class: hibernate.Object
at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:2036)
at org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2497)
at org.hibernate.cfg.HbmBinder$SecondPass.doSecondPass(HbmBinder.java:2468)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:884)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:999)
at hibernate.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:49)
at hibernate.HibernateTest.main(HibernateTest.java:27)
Exception in thread "main" java.lang.NullPointerException
at hibernate.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:56)
at hibernate.HibernateTest.main(HibernateTest.java:27)
könnt ihr mir helfen?