You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by "Plummer, Greg" <Gr...@LibertyMutual.com> on 2003/08/11 20:17:47 UTC

Problem using anonymous keys

Hi All,

This is my second post regarding this problem. I was able to get the insert to work fine, but when I changed the test to use anonymous keys, I continue to get the following problem. Code and repository.xml are attached below. Any help would be appreciated.

Thanks,
Greg Plummer

assertValidPkFields failed for Object of type: ojbtests.Client on insert
org.apache.ojb.broker.PersistenceBrokerException: assertValidPkFields failed for Object of type: ojbtests.Client on insert
	at org.apache.ojb.broker.core.PersistenceBrokerImpl.storeToDb(PersistenceBrokerImpl.java:1942)
	at org.apache.ojb.broker.core.PersistenceBrokerImpl.store(PersistenceBrokerImpl.java:1889)
	at org.apache.ojb.broker.core.PersistenceBrokerImpl.store(PersistenceBrokerImpl.java:665)
	at org.apache.ojb.broker.core.DelegatingPersistenceBroker.store(DelegatingPersistenceBroker.java:160)
	at org.apache.ojb.broker.core.DelegatingPersistenceBroker.store(DelegatingPersistenceBroker.java:160)
	at ojbtests.OjbTest.createClient(OjbTest.java:39)
	at ojbtests.OjbTest.createClients(OjbTest.java:62)
	at ojbtests.OjbTest.testInsert(OjbTest.java:96)
	at java.lang.reflect.Method.invoke(Native Method)
	at junit.framework.TestCase.runTest(TestCase.java:154)
	at junit.framework.TestCase.runBare(TestCase.java:127)
	at junit.framework.TestResult$1.protect(TestResult.java:106)
	at junit.framework.TestResult.runProtected(TestResult.java:124)
	at junit.framework.TestResult.run(TestResult.java:109)
	at junit.framework.TestCase.run(TestCase.java:118)
	at junit.framework.TestSuite.runTest(TestSuite.java:208)
	at junit.framework.TestSuite.run(TestSuite.java:203)
	at junit.textui.TestRunner.doRun(TestRunner.java:116)
	at junit.textui.TestRunner.doRun(TestRunner.java:109)
	at junit.textui.TestRunner.run(TestRunner.java:72)
	at junit.textui.TestRunner.run(TestRunner.java:57)
	at ojbtests.OjbTest.main(OjbTest.java:77)


Domain Classes:

package ojbtests;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Client {
	
	private String name;
	private int id;
	private Collection clientGroups = new ArrayList();

	public Client() {
		super();
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

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

	public void setName(String name) {
		this.name = name;
	}
	
	public void addClientGroup(ClientGroup clientGroup) {
		clientGroups.add(clientGroup);
		clientGroup.setClient(this);
	}
	
	public Iterator getClientGroups() {
		return clientGroups.iterator();
	}
}

package ojbtests;

public class ClientGroup {
	
	private String name;
	private int id;
	private Client client;

	public ClientGroup() {
		super();
	}

	public Client getClient() {
		return client;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setClient(Client client) {
		this.client = client;
	}

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

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

DB Setup code:

	private void create(Connection conn) throws SQLException {

		Statement statement=conn.createStatement();
		try {
			statement.executeUpdate("DROP TABLE client_group");
			statement.executeUpdate("DROP TABLE client");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		statement.execute("CREATE TABLE client(" +
			"id INTEGER PRIMARY KEY, " +
			"name VARCHAR(255))");
		statement.execute(
			"CREATE TABLE client_group(" +
				"id INTEGER PRIMARY KEY, "  +
				"name VARCHAR(255), " +
				"client_id INTEGER, " +
				"FOREIGN KEY (client_id) REFERENCES client(id))");
		statement.close();
	}

Repository.xml file:

   <class-descriptor
   	  class="ojbtests.Client"
   	  table="CLIENT"
   >
      <field-descriptor
         name="id"
         column="ID"
         jdbc-type="INTEGER"
         primarykey="true"
         autoincrement="false"
         access="anonymous"
      />
      <field-descriptor
         name="name"
         column="NAME"
         jdbc-type="VARCHAR"
      />
      <collection-descriptor
      	name="clientGroups"
      	element-class-ref="ojbtests.ClientGroup"
      >
      		<inverse-foreignkey field-ref="clientId" />
      </collection-descriptor>
   </class-descriptor>

   <class-descriptor
   	  class="ojbtests.ClientGroup"
   	  table="CLIENT_GROUP"
   >
      <field-descriptor
         name="id"
         column="ID"
         jdbc-type="INTEGER"
         primarykey="true"
         autoincrement="false"
      />
      <field-descriptor
         name="name"
         column="NAME"
         jdbc-type="VARCHAR"
      />
      <field-descriptor
         name="clientId"
         column="CLIENT_ID"
         jdbc-type="INTEGER"
         access="anonymous"
      />
   </class-descriptor>