I try to learn JDO and I have some problems already. After I succeeded to configure datanucleus with spring using HSQLDB, I started to play around.

I have a Worker class and a Tool class. A Worker can have more than one tool so we have a 1-n relationship.

There are 2 ways to define a 1-n relationship. One case is with a foreign key and the other is throw a middle join table.

Here the solution with the foreign key (using Annotations):


public class Worker {


@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;

@Override
public String toString() {
return this.getClass().getName() + " [" +
"id:"+id+", " +
"tools:"+getTools()+", " +
"]";
}

@Persistent
private String name;

@Persistent
@Element(types=gr.open.thohapi.model.Tool.class)
private Set tools = new HashSet();
.
.
.
}


and here the solution with the join table (using Annotations):

public class Worker {


@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;

@Override
public String toString() {
return this.getClass().getName() + " [" +
"id:"+id+", " +
"tools:"+getTools()+", " +
"]";
}

@Persistent
private String name;

@Persistent
@Element(types=gr.open.thohapi.model.Tool.class)
@Join(table="WORKER_TOOLS", column="WORKER_ID")
private Set tools = new HashSet();
.
.
.
}

0 comments: