Hello,
when I save the object it raised the following exception:
Code:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: mo.kudocs.framework.bean.User
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:108)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:221)
at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:476)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:2802)
at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:457)
at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:180)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:104)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:195)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:35)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.prepareQueries(SessionImpl.java:1071)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1061)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1010)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at mo.kudocs.framework.dao.SkinDAO.findByName(SkinDAO.java:72)
at mo.kudocs.framework.manager.SkinManager.getSkin(SkinManager.java:62)
at mo.kudocs.framework.servlet.KudocsInitializer.createAdmin(KudocsInitializer.java:251)
at mo.kudocs.framework.servlet.KudocsInitializer.initUsers(KudocsInitializer.java:159)
at mo.kudocs.framework.servlet.KudocsInitializer.init(KudocsInitializer.java:83)
at mo.kudocs.framework.servlet.KudocsInitializer.init(KudocsInitializer.java:73)
at mo.kudocs.framework.servlet.KudocsInitializerListener.contextInitialized(KudocsInitializerListener.java:71)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3669)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4104)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1012)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1012)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
at org.apache.catalina.core.StandardService.start(StandardService.java:450)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:683)
at org.apache.catalina.startup.Catalina.start(Catalina.java:537)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:271)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:409)
Code:
private static void createAdmin() throws Exception {
Session session = null;
UserDAO userDAO = null;
try {
// kudocsadmin
userDAO = new UserDAO();
User user = null;
try {
session = HibernateUtil.getSession();
HibernateUtil.beginTransaction();
user = (User) userDAO.findByUsername("kudocsadmin");
HibernateUtil.rollback();
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug("kudocsadmin not found: ", e);
}
}
if (user == null) {
session = HibernateUtil.getSession();
HibernateUtil.beginTransaction();
//long skinID = SkinManager.getSkin("classic").getId();
user = new User();
// user.setId(SystemCounterDAO.nextId(GlobalConstant.USER_COUNTER));
user.setStatus(StatusDAO.ACTIVE());
// UserSecurity
UserSecurity us = new UserSecurity();
// us.setId(SystemCounterDAO.nextId(GlobalConstant.USERSECURITY_COUNTER));
us.setUsername("kudocsadmin");
// us.setId(SystemCounterDAO.nextId(GlobalConstant.USERSECURITY_COUNTER));
String seed = Password.getSeed();
us.setSeed(seed);
us.setPassword(Password.newCrypt("123456", seed));
us.setUser(user);
us.setCheckIp(false);
user.setUserSecurity(us);
HibernateUtil.insert(session, us);
// UserPreferences / PreferenceNode
UserPreferences userPref = new UserPreferences();
PreferenceNode prefNode = new PreferenceNode();
long skinID = SkinManager.getSkin(session, "classic").getId();
prefNode.putNode(GlobalConstant.PREFERENCE_SKIN, ""+skinID);
userPref.addPreferenceNode(prefNode);
prefNode.setPreferences(userPref);
HibernateUtil.insert(session, prefNode);
HibernateUtil.insert(session, userPref);
userPref.setOwner(user);
user.setUserPreferences(userPref);
// UserGroup
UserGroup ug = new UserGroup();
// ug.setId(SystemCounterDAO.nextId(GlobalConstant.GROUP_COUNTER));
ug.setName("kudocsadmin");
ug.setDescription("The KuDocs Adminstration group");
ug.addUser(user);
user.addGroup(ug);
HibernateUtil.insert(session, ug);
// UserRole
UserRole ur = new UserRole();
// ur.setId(SystemCounterDAO.nextId(GlobalConstant.GROUP_COUNTER));
ur.setName("kudocsadmin");
ur.setDescription("The KuDocs Administration role");
ur.addUser(user);
user.addRole(ur);
HibernateUtil.insert(session, ur);
UserRole ur1 = new UserRole();
ur1.setName("kudocsuser");
ur1.setDescription("The KuDocs User role");
HibernateUtil.insert(session, ur1);
// UserInfo
UserInfo info = new UserInfo();
// info.setId(SystemCounterDAO.nextId(GlobalConstant.USERINFO_COUNTER));
info.setFirstName("KuDocs");
info.setLastName("KuDocs");
info.setSex(GlobalConstant.MALE);
info.setBirth(DateTimeUtil.formatDate(DateTimeUtil.formatDate(
new Date(), "dd/MM/yyyy"), "dd/MM/yyyy"));
info.setOwner(user);
HibernateUtil.insert(session, info);
// address
Address addr = new Address();
// addr.setId(SystemCounterDAO.nextId(GlobalConstant.ADDRESS_COUNTER));
addr.setStreet1("KuDocs Tower");
addr.setCity("MACAU S.A.R.");
addr.setCountry("CHINA PRC");
addr.setUserInfo(info);
HibernateUtil.insert(session, addr);
info.setAddress(addr);
user.setUserInfo(info);
HibernateUtil.insert(session, user);
HibernateUtil.commit();
log.info("===== INITIALIZE ADMINISTRATOR =====");
}
} catch (Exception e) {
HibernateUtil.rollbackTransaction();
log.error("Create kudocsadmin failed: ", e);
} finally {
HibernateUtil.close();
}
}
Code:
/**
* SkinManager.java
*
* Copyright (C) 2005 Eric Chow. All right Reserved.
*
* This software is the confidential and proprietary information of Eric Chow.
*/
package mo.kudocs.framework.manager;
import java.util.Collection;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import mo.kudocs.framework.GlobalConstant;
import mo.kudocs.framework.bean.Skin;
import mo.kudocs.framework.bean.SystemProperty;
import mo.kudocs.framework.dao.AbstractDAO;
import mo.kudocs.framework.dao.DAOUtil;
import mo.kudocs.framework.dao.SkinDAO;
import mo.kudocs.framework.dao.SystemPropertyDAO;
import mo.kudocs.framework.util.HibernateUtil;
/**
* SkinManager.java
*
* @author Chao Hoi Ka, Eric
*
*/
public class SkinManager {
private static Log log = LogFactory.getLog(SkinManager.class.getName());
private static Skin defaultSkin = null;
/**
*
*/
public SkinManager() {
super();
}
public static Skin getSkin(String name) {
try {
return getSkin(HibernateUtil.getSession(), name);
} catch(Exception e) {
log.error("get skin failed", e);
return null;
}
}
public static Skin getSkin(Session session, String name) {
SkinDAO skinDAO = null;
Skin skin = null;
try {
skinDAO = new SkinDAO(session);
skin = (Skin)skinDAO.findByName(name);
} catch(Exception e) {
log.error("get skin failed", e);
}
return skin;
}
public static Skin getDefaultSkin() {
if (defaultSkin == null) {
SystemPropertyDAO spDAO = null;
long defaultSkinID = -1;
try {
spDAO = new SystemPropertyDAO();
SystemProperty sp = (SystemProperty)spDAO.findByName(GlobalConstant.PREFERENCE_SKIN_DEFAULT);
if (sp != null) {
defaultSkinID = Long.parseLong(sp.getValue());
}
} catch(Exception e) {
log.error("get default skin failed", e);
}
SkinDAO skinDAO = null;
try {
skinDAO = new SkinDAO();
if (defaultSkinID == -1) {
Collection allSkins = getAllSkins();
if (allSkins != null && !allSkins.isEmpty()) {
defaultSkin = (Skin)allSkins.iterator().next();
}
} else {
defaultSkin = (Skin)skinDAO.getById(defaultSkinID);
}
} catch(Exception e) {
log.error("get default skin failed", e);
}
}
return defaultSkin;
}
public static Collection getAllSkins() {
SkinDAO skinDAO = null;
Collection skins = null;
try {
skinDAO = new SkinDAO();
skins = skinDAO.findAll();
} catch(Exception e) {
log.error("get all skin", e);
}
return skins;
}
}