mchyzer wrote:
Then the object will have the id in it as well, which obviously isnt happening. So fix that problem and you are all set.
But this is the exact problem! I do not know why Hibernate does not assign the (generated) id to the object. I thought it has something to do with MySQL and tried HSQLDB instead, but no effect. The problem remains.
I wrote a second testcase which does not use the Session-Local-Thread-Pattern:
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import de.persistencemanager.util.InfrastructureException;
import de.soccermanager.constant.Constant;
import de.soccermanager.constant.enumeratedtypes.Gender;
import de.soccermanager.person.entity.Address;
import de.soccermanager.person.entity.Player;
import de.soccermanager.person.entity.TeamManager;
import de.soccermanager.team.entity.Team;
import junit.framework.TestCase;
/**
* @author sven
*
* TODO Kurzbeschreibung der Klasse
*/
public class TestInsertDataWithoutHibernateUtil extends TestCase {
Player player = new Player();
TeamManager teamManager = new TeamManager();
Team team = new Team();
Address address = null;
Session session;
Transaction tx;
static Configuration configuration;
static SessionFactory sessionFactory;
static {
try {
configuration = new Configuration();
sessionFactory = configuration.configure().buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
team.setStrTeamName("HSV");
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}
/**
* Constructor for TestInsertData.
* @param name
*/
public TestInsertDataWithoutHibernateUtil(String name) {
super(name);
}
public void testInsertTeamManager() throws InfrastructureException{
assertNotNull("teamManager == null", teamManager);
try {
session = sessionFactory.openSession();
tx = session.beginTransaction();
} catch (HibernateException e) {
e.printStackTrace();
}
teamManager.setStrFirstName("Thomas");
...
address = new Address();
address.setStrStreet("Schreyerstr.");
...
teamManager.setAddress(address);
team.setTeamManager(teamManager);
teamManager.setTeam(team);
try {
session.save(teamManager);
tx.commit();
} catch (HibernateException e1) {
e1.printStackTrace();
}
finally{
try {
session.close();
} catch (HibernateException e2) {
e2.printStackTrace();
}
}
}
public void testInsertPlayer() throws InfrastructureException{
try {
session = sessionFactory.openSession();
tx = session.beginTransaction();
} catch (HibernateException e) {
e.printStackTrace();
}
player = new Player();
address = new Address();
player.setStrFirstName("Diego";
...
address.setStrStreet("Hollywood Bvd");
...
player.setAddress(address);
assertNotNull("teamId == null", team.getId()); //fails!!!!!
team.addPlayer(player);
assertNotNull("team == null", player.getTeam());
try {
session.save(player);
tx.commit();
} catch (HibernateException e1) {
e1.printStackTrace();
}
finally{
try {
session.close();
} catch (HibernateException e2) {
e2.printStackTrace();
}
}
}
}
The highlighted line is an unexpected result. I would assume, after I have inserted TeamManager (Team is being inserted automatically then..) that Hibernate assigned the id to this object via reflection. But obviously this is not the case. The id remains null and therefore - I conclude - Hibernate will insert the Team object again instead of updating it. Why is it not assigned?!
This must be trivial, but I cannot find my mistake. Any help is appreciated.
Sven