users@glassfish.java.net

Re: Basic JPA Question

From: <glassfish_at_javadesktop.org>
Date: Thu, 18 Oct 2007 10:26:28 PDT

I probably misunderstood your problem entirely. But using generated ID:s shouldn't be that hard as long as the ID of the CPU is just an internal ID (not shown to users or whatever clients there is) used to uniquely identify specific CPU entities. If you want to change a CPU entity for a server or remove it you either search for its external ID or use its internal ID to find it directly. But if you want unique external CPU ID:s you have to make sure that you either create a constraint in your database or just be extra careful when you code.

@Entity
public class Server {
@Id
@GeneratedValue
private int id;
@OneToMany(cascade=Cascade.ALL)
@JoinColumn(name="server_id")
private Set cpus;
...
}

@Entity
public class CPU {
@Id
@GeneratedValue
private int id;
private int actualCPUId;
...
}

In code using entities:

Server server = entityManager.find(Server.class, 1);
server.getCPUS().add(...);
server.getCPUS().remove(...);
// etc
entityManager.merge(server);
[Message sent by forum member 'woel' (woel)]

http://forums.java.net/jive/thread.jspa?messageID=240995