Can you post any sample code? This is the code I am using, and I am able to
create grandchild objects:
/* Domain classes. Imports implied */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class League {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String name;
@Persistent
private List<Team> teams;
public League(String name) {
this.name = name;
this.teams = new ArrayList<Team>();
}
public League(String name, List<Team> teams) {
this.name = name;
this.teams = teams;
}
public Key getKey() {
return key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Team> getTeams() {
return teams;
}
public void setTeams(List<Team> teams) {
this.teams = teams;
}
@Override
public String toString() {
return "League{" +
"key=" + key +
", name='" + name + '\'' +
", teams=" + teams +
'}';
}
}
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Team {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String name;
@Persistent
private List<Player> players;
public Team(String name) {
this.name = name;
this.players = new ArrayList<Player>();
}
public Team(String name, List<Player> players) {
this.name = name;
this.players = players;
}
public Key getKey() {
return key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Player> getPlayers() {
return players;
}
public void setPlayers(List<Player> players) {
this.players = players;
}
@Override
public String toString() {
return "Team{" +
"key=" + key +
", name='" + name + '\'' +
", players=" + players +
'}';
}
}
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Player {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String name;
public Player(String name) {
this.name = name;
}
public Key getKey() {
return key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Player{" +
"key=" + key +
", name='" + name + '\'' +
'}';
}
}
/* Servlet test classes, again, imports implied */
public class AddLeagueTeamPlayerServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String leagueName = request.getParameter("leagueName");
String teamName = request.getParameter("teamName");
String playerName = request.getParameter("playerName");
Player player = new Player(playerName);
ArrayList<Player> players = new ArrayList<Player>();
players.add(player);
Team team = new Team(teamName, players);
ArrayList<Team> teams = new ArrayList<Team>();
teams.add(team);
League league = new League(leagueName, teams);
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
pm.makePersistent(league);
} finally {
pm.close();
}
response.getWriter().println("Created League: " + league);
}
}
public class AddPlayerToTeamServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String teamName = request.getParameter("teamName");
String playerName = request.getParameter("playerName");
Player player = new Player(playerName);
Team team;
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
Query query = pm.newQuery(Team.class);
query.setFilter("name == teamNameParam ");
query.declareParameters("String teamNameParam");
@SuppressWarnings("unchecked")
List<Team> teams = (List<Team>) query.execute(teamName);
team = teams.get(0);
team.getPlayers().add(player);
pm.makePersistent(team);
response.getWriter().println("Added player " + player + " to
team. New team: " + team);
} finally {
pm.close();
}
}
}
On Thu, Nov 26, 2009 at 6:36 AM, Icarus <[email protected]> wrote:
> Hi,
>
> I have a Parent Class with a List of Child Objects. This is an owned
> one to many relation and I am able to get the child objects from the
> Parent Object after I get the Parent Object from the DataStore.
>
> Now, the Child Objects in turn have a List of Objects ( so they are
> the grandchildren)... now when I create a GrandChild object and try to
> call makePersistent() on it, they key is always null.
>
> Is there something very basic that I am missing ?
>
> Thanks,
> -Ic
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "Google App Engine for Java" group.
> To post to this group, send email to
> [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-appengine-java%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-appengine-java?hl=en.
>
>
>
--
Ikai Lan
Developer Programs Engineer, Google App Engine
--
You received this message because you are subscribed to the Google Groups
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.