IllegalArgumentException mit Hibernate und Tabellenrelation



Hallöchen!

Ich hoffe, dass ich mich am Schluß nicht ärgere, weil es irgendwas
Triviales war, sondern ich wirklich was lerne, weil ich was falsch
verstanden habe ;-)

Ich will mein Java wieder auffrischen. Um das zu tun, brauche ich
natürlich ein Projekt, sonst wird das nix.
Als Projekt hab ich mir eine kleine Aufgabenverwaltung auserkoren.

Als zentrale Tabelle gibts in der Oracle-Datenbank die Tabelle TASK. Die
soll über Fremdschlüssel dann den Bearbeiter, die Kategorie, den Kunden
und den Status zugewiesen bekommen.

Somit gibt es folgende Klassen:
Agent - die Bearbeiter
Category - die Kategorien (also z.B. ADMIN, DEV, ..)
Customer - die Kunden
Status - die Stati
Task - die Aufgaben

Bisher hab ich dann einfach immer mit SQL-Statements gearbeitet und das
manuell gemacht... diesmal wollte ich was anderes probieren und was
Neues lernen ;-) Bisher habe ich nur eine einzige Verbindung (versucht)
herzustellen, und zwar Task mit Agent.
Erstmal soll eine Aufgabe auch nur mit von einem Bearbeiter gemacht
werden.

Ich habe einen kleinen Test geschrieben, der leider immer mit der
Exception "IllegalArgumentException" fehlschlägt.

Diese Exception kommt immer dann, wenn - oh Überraschung - ein Argument
falsch ist. Hier wird allerdings die "zav.taskmanagement.Agent, getter
method of property: id" angemeckert. Der Getter dafür erwartet keinen
Parameter, gibt aber einen String zurück (nämlich das Kürzel, was im
Feld ID ist).

Wo ist da jetzt dann das Problem?
Wenn ich mir die Datenbank-Tabelle anschaue, so wird da völlig richtig
eine 2-Zeichen Spalte angelegt. ... und mehr versuche ich da auch gar
nicht reinzupacken bei dem simplen Beispiel.


Irgendwie steh ich total aufm Schlauch, und hab keine Ahnung, wo mein
Fehler sein könnte... wenn sich jemand die Zeit genommen hat, und das
Beispiel hier schonmal überhaupt angesehen hat: Herzlichen Dank dafür!
Wer auch noch die Lösung hat, und sie nicht platt hinschreibt, sondern
mir dezente Tips gibt (will ja auch selbst noch Grips verwenden), der
verdient erst recht ein großes DANKE!


Liebe Grüße,
Viktor


Im folgenden noch die Dateien:
Agent.java
Task.java
agent.hbm.xml
task.hbm.xml
.... sollte was fehlen, kann ich das auch nachliefern.


Liebe Grüße,
Viktor



Agent.java
----------
package zav.taskmanagement;

import java.util.*;

public class Agent {
private String id;
private String name;
private boolean active;
private Set tasks = new HashSet();

public String getId() {
return id;
}

public void setId(String pId) {
this.id = pId;
}

public String getName() {
return name;
}

public void setName(String pName) {
this.name = pName;
}

public boolean isActive() {
return active;
}

public void setActive(boolean pActive) {
this.active = pActive;
}

public Set getTasks() {
return tasks;
}

public void setTasks(Set pTasks) {
this.tasks = pTasks;
}
}


Task.java
---------
package zav.taskmanagement;

import java.util.Iterator;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import junit.framework.TestCase;

public class TestTask extends TestCase {
private int id;
SessionFactory sessionFactory;

protected void setUp() throws Exception {
super.setUp();
Configuration configuration = new
Configuration().configure();
SchemaExport export = new SchemaExport(configuration);
export.create(false, true);

sessionFactory = configuration.buildSessionFactory();
}

private String erzeugeAgent(String pId, String pName, boolean
pActive) {
Agent agent = new Agent();
agent.setId(pId);
agent.setName(pName);
agent.setActive(pActive);
Session session = null;
Transaction transaction = null;

try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
session.save(agent);
transaction.commit();
} catch (HibernateException e) {
if (transaction != null) {
transaction.rollback();
throw e;
}
} finally {
if (session != null) {
session.close();
}
}
return agent.getId();
}

private int erzeugeTask(String pAgentId, String pText) {
Task task = new Task();
// task.getAgents().add(pAgent);
task.setText(pText);
Session session = null;
Transaction transaction = null;

try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();

Iterator itrAgent = session.createQuery("select agent from
Agent as agent where agent.id = '" + pAgentId + "'" ).iterate();
if( itrAgent.hasNext() ) {
Agent agent = (Agent) itrAgent.next();
//Task task = (Task) itrTask.next();
task.getAgents().add( agent );
} else {
System.out.println( "\nFehler:
Bearbeiter unbekannt." );
}

//Agent dummy = (Agent)
task.getAgents().iterator();
//System.out.println("Agent: " + dummy.getId()
);

System.out.println("vor Fehler");
session.save(task); // <- hier fehler
System.out.println("nach Fehler ;-)");
transaction.commit();
} catch (HibernateException e) {
if (transaction != null) {
transaction.rollback();
throw e;
}
} finally {
if (session != null) {
session.close();
}
}
return task.getId();
}

public void testLoad() {
Session session = null;
Task task = null;
Agent agent = null;

String agentid = "";

String lAgentId = "VZ";
String lAgentName = "ViktorX";

try {
session = sessionFactory.openSession();
agentid = erzeugeAgent(lAgentId, lAgentName,
true);
agent = (Agent) session.load(Agent.class,
lAgentId);
assertEquals(lAgentId, agent.getId());
assertEquals(lAgentName, agent.getName());
} finally {
if (session != null && session.isConnected()) {
session.close();
}
}

try {
session = sessionFactory.openSession();
id = erzeugeTask(lAgentId, "Test-Aufgabe");
task = (Task) session.load(Task.class, id);
//assertEquals(lAgentId, task.getAgentId());
assertEquals("Test-Aufgabe", task.getText());
} finally {
if (session != null && session.isConnected()) {
session.close();
}
}
}
}


TestTask.java
-------------
package zav.taskmanagement;

import java.util.Iterator;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import junit.framework.TestCase;

public class TestTask extends TestCase {
private int id;
SessionFactory sessionFactory;

protected void setUp() throws Exception {
super.setUp();
Configuration configuration = new
Configuration().configure();
SchemaExport export = new SchemaExport(configuration);
export.create(false, true);

sessionFactory = configuration.buildSessionFactory();
}

private String erzeugeAgent(String pId, String pName, boolean
pActive) {
Agent agent = new Agent();
agent.setId(pId);
agent.setName(pName);
agent.setActive(pActive);
Session session = null;
Transaction transaction = null;

try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
session.save(agent);
transaction.commit();
} catch (HibernateException e) {
if (transaction != null) {
transaction.rollback();
throw e;
}
} finally {
if (session != null) {
session.close();
}
}
return agent.getId();
}



private int erzeugeTask(String pAgentId, String pText) {
Task task = new Task();
task.setText(pText);
Session session = null;
Transaction transaction = null;

try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();

Iterator itrAgent = session.createQuery("select agent from
Agent as agent where agent.id = '" + pAgentId + "'" ).iterate();
if( itrAgent.hasNext() ) {
Agent agent = (Agent) itrAgent.next();
task.getAgents().add( agent );
} else {
System.out.println( "\nFehler:
Bearbeiter unbekannt." );
}

session.save(task); // IllegalArgumentException
bei .save
transaction.commit();
} catch (HibernateException e) {
if (transaction != null) {
transaction.rollback();
throw e;
}
} finally {
if (session != null) {
session.close();
}
}
return task.getId();
}

public void testLoad() {
Session session = null;
Task task = null;
Agent agent = null;

String agentid = "";
String lAgentId = "VZ";
String lAgentName = "Viktor";

try {
session = sessionFactory.openSession();
agentid = erzeugeAgent(lAgentId, lAgentName,
true);
agent = (Agent) session.load(Agent.class,
lAgentId);
assertEquals(lAgentId, agent.getId());
assertEquals(lAgentName, agent.getName());
} finally {
if (session != null && session.isConnected()) {
session.close();
}
}

try {
session = sessionFactory.openSession();
id = erzeugeTask(lAgentId, "Test-Aufgabe");
task = (Task) session.load(Task.class, id);
assertEquals("Test-Aufgabe", task.getText());
} finally {
if (session != null && session.isConnected()) {
session.close();
}
}
}
}

agent.hbm.xml
-------------
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD
3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd";>
<hibernate-mapping package="zav.taskmanagement">
<class name="Agent">
<id name="id" length="2">
<generator class="assigned"></generator>
</id>
<property name="name" length="100"></property>
<property name="active" type="boolean"></property>
<set name="tasks" lazy="true">
<key column="agent_id"></key>
<one-to-many class="Task" />

</set>
</class></hibernate-mapping>


task.hbm.xml
------------
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD
3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd";><hibernate-
mapping package="zav.taskmanagement">
<class name="Task">
<id name="id" type="int">
<generator class="native"></generator>
</id>
<many-to-one name="agents" column="agent_id"
class="Agent">
</many-to-one>
<property name="text" length="1000"></property>
</class></hibernate-mapping>
.



Relevant Pages

  • Re: Cross Transactions between ADO & ADO.Net
    ... The issue of one result-set at a time has to do with transaction ... Can this restriction be gotten around using MARS? ... > MARS session pool limited to one connection?). ... >>> require two connections to the DB and if possible we would like a ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: bind() udp behavior 2.6.8.1
    ... "Transaction ID" in the DNS porttion of the packet. ... > ip:port and from no other server on the net. ... You new session is totally ...
    (Linux-Kernel)
  • Re: Access 2000 & Stored Procedures
    ... Inside the transaction you'll select the record to call, ... >I'm running a application based on Access 2000 and SQL ... >stored procedure with a variable of the agent's ID. ... >record with the agent ID. ...
    (microsoft.public.sqlserver.clients)
  • Re: deadlocks between single update statements ?
    ... I guess one could think of the serializable transaction isolation ... session 2 gets row B ... session 1 detects deadlock and rolls back select statement. ... pretty widespread changes in the code base. ...
    (comp.databases.oracle.server)
  • Whats Your Lamest Gaming Day?
    ... Session 1 of 2: Make contact with an agent in a war torn border town. ... A four hour game over in one hour. ... What were your lamest gaming days from the perspective of a player? ...
    (rec.games.frp.dnd)