Skip to content

Commit

Permalink
update to use internal PK objectid-class to avoid use of DN classes
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjefferson committed Jun 10, 2014
1 parent 568d4e1 commit a272a24
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 16 deletions.
1 change: 1 addition & 0 deletions many_to_many_attributed/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/bin
/target
/datanucleus.log
1 change: 1 addition & 0 deletions many_to_many_attributed/.project
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.datanucleus.ide.eclipse.projectnature</nature>
</natures>
</projectDescription>
2 changes: 1 addition & 1 deletion many_to_many_attributed/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
<systemProperties>
<systemProperty>
<key>log4j.configuration</key>
<value>file:${basedir}/log4j.properties</value>
<value>file:${basedir}/src/main/resources/log4j.properties</value>
</systemProperty>
</systemProperties>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.io.Serializable;
import java.util.StringTokenizer;

import org.datanucleus.identity.LongId;

//@PersistenceCapable
public class BusinessRelation
{
Expand Down Expand Up @@ -69,23 +67,23 @@ public String getMeetingLocation()
*/
public static class PK implements Serializable
{
public LongId customer; // Use same name as field above
public LongId supplier; // Use same name as field above
public Customer.PK customer; // Use same name as field above
public Supplier.PK supplier; // Use same name as field above

public PK()
{
}

public PK(String s)
{
StringTokenizer st = new StringTokenizer(s, "::");
this.customer = new LongId(Customer.class, st.nextToken());
this.supplier = new LongId(Supplier.class, st.nextToken());
StringTokenizer st = new StringTokenizer(s, "--");
this.customer = new Customer.PK(st.nextToken());
this.supplier = new Supplier.PK(st.nextToken());
}

public String toString()
{
return (customer.toString() + "::" + supplier.toString());
return (customer.toString() + "--" + supplier.toString());
}

public int hashCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
**********************************************************************/
package org.datanucleus.samples.jdo.many_many_attributed;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

//@PersistenceCapable
public class Customer
{
//@PrimaryKey
long id;

String name = null;
Expand Down Expand Up @@ -60,4 +60,48 @@ public String toString()
{
return "Customer : " + name + " - " + supplierRelations.size() + " suppliers";
}

public static class PK implements Serializable
{
public long id;

public PK()
{
}

public PK(java.lang.String str)
{
java.util.StringTokenizer token = new java.util.StringTokenizer(str, "::");
token.nextToken(); // Class name
this.id = Long.valueOf(token.nextToken());
}

public java.lang.String toString()
{
return Customer.class.getName() + "::" + java.lang.String.valueOf(this.id);
}

public int hashCode()
{
return (int) id;
}

public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (o == null)
{
return false;
}
if (o.getClass() != getClass())
{
return false;
}
PK objToCompare = (PK) o;
return ((this.id == objToCompare.id));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Transaction;

import org.datanucleus.util.NucleusLogger;

public class Main
{
public static void main(String[] args)
{
System.out.println("DataNucleus Samples : M-N Relation (attributed)");
System.out.println("===============================================");
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("datanucleus.properties");
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("MyTest");

Customer cust1 = null;
Customer cust2 = null;
Expand All @@ -39,6 +41,7 @@ public static void main(String[] args)
// Persist some objects
System.out.println(">> Persisting some Customers and Suppliers");
PersistenceManager pm = pmf.getPersistenceManager();
pm.getFetchPlan().setGroup("all");
Transaction tx = pm.currentTransaction();
try
{
Expand All @@ -61,7 +64,7 @@ public static void main(String[] args)
}
catch (Exception e)
{
e.printStackTrace();
NucleusLogger.GENERAL.error(">> Exception in persist", e);
System.exit(1);
}
finally
Expand All @@ -88,6 +91,7 @@ public static void main(String[] args)

pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
pm.getFetchPlan().setGroup("all");
try
{
tx.begin();
Expand All @@ -101,7 +105,7 @@ public static void main(String[] args)
}
catch (Exception e)
{
e.printStackTrace();
NucleusLogger.GENERAL.error(">> Exception in reattach", e);
System.exit(2);
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
**********************************************************************/
package org.datanucleus.samples.jdo.many_many_attributed;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

//@PersistenceCapable
public class Supplier
{
//@PrimaryKey
long id;

String name = null;
Expand All @@ -43,26 +43,86 @@ public String getName()

public void addRelation(BusinessRelation rel)
{
if (customerRelations == null)
{
customerRelations = new HashSet<BusinessRelation>();
}
customerRelations.add(rel);
}

public void removeRelation(BusinessRelation rel)
{
if (customerRelations == null)
{
return;
}
customerRelations.remove(rel);
}

public Set<BusinessRelation> getRelations()
{
if (customerRelations == null)
{
customerRelations = new HashSet<BusinessRelation>();
}
return customerRelations;
}

public int getNumberOfRelations()
{
if (customerRelations == null)
{
return 0;
}
return customerRelations.size();
}

public String toString()
{
return "Supplier : " + name + " - " + customerRelations.size() + " customers";
}

public static class PK implements Serializable
{
public long id;

public PK()
{
}

public PK(java.lang.String str)
{
java.util.StringTokenizer token = new java.util.StringTokenizer(str, "::");
token.nextToken(); // Class name
this.id = Long.valueOf(token.nextToken());
}

public java.lang.String toString()
{
return Supplier.class.getName() + "::" + java.lang.String.valueOf(this.id);
}

public int hashCode()
{
return (int) id;
}

public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (o == null)
{
return false;
}
if (o.getClass() != getClass())
{
return false;
}
PK objToCompare = (PK) o;
return ((this.id == objToCompare.id));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<property name="javax.jdo.option.ConnectionPassword" value=""/>
<property name="javax.jdo.option.Mapping" value="h2"/>

<property name="datanucleus.DetachAllOnCommit" value="true"/>
<property name="datanucleus.schema.autoCreateAll" value="true"/>
</properties>
</persistence-unit>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
"http://java.sun.com/dtd/jdo_2_0.dtd">
<jdo>
<package name="org.datanucleus.samples.jdo.many_many_attributed">
<class name="Customer" detachable="true" table="CUSTOMER">
<class name="Customer" detachable="true" table="CUSTOMER" objectid-class="Customer$PK">
<field name="id" primary-key="true" value-strategy="increment" column="ID"/>
<field name="name" column="NAME"/>
<field name="supplierRelations" persistence-modifier="persistent" mapped-by="customer">
<collection element-type="BusinessRelation"/>
</field>
</class>

<class name="Supplier" detachable="true" table="SUPPLIER">
<class name="Supplier" detachable="true" table="SUPPLIER" objectid-class="Supplier$PK">
<field name="id" primary-key="true" value-strategy="increment" column="ID"/>
<field name="name" column="NAME"/>
<field name="customerRelations" persistence-modifier="persistent" mapped-by="supplier">
Expand Down

0 comments on commit a272a24

Please sign in to comment.