users@glassfish.java.net

Re: persistence - a whole series of *funky* errors

From: <glassfish_at_javadesktop.org>
Date: Fri, 10 Aug 2007 12:59:19 PDT

JPA 1.0 Specification, section 2.1.4 "The primary key (or field or property of a composite primary key) should be one of the following types: any Java primitive type; any primitive wrapper type; java.lang.String; java.util.Date; java.sql.Date. In general, however, approximate numeric types (e.g., floating point types) should never be used in primary keys. Entities whose primary keys use types other than these will not be portable."

Having an Entity within the Primary Key of another Entity is not supported by the specification and would not be portable across persistence providers. There are means (multiple mappings with one of the mappings being marked as not updatable not insertable) to be spec complaint and still have your relationships form a component of the PK.

@Entity
public class GCharacter { // "character" is an sql reserved word :-(
  @Embeddable
  public static class PK {
    private String game_name;
    private String name;

    @Column(name="GAME", updatable=false, insertable=false)
    public String getGameName() { return game_name; }
    public void setGameName(String game_name) { this.game_name = game_name; }

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
  }
 
 
  private PK pk;
  private Player player;
  private Game game;

  //PK should either be created in a Constructor or lazy initialized.
  @Id
  public PK getPk() { return pk; }
  public void setPk(PK pk) { this.pk = pk;}
 
  @ManyToOne
  @JoinColumn(name="GAME")
  public Game getGame() { return game;}
  public void setGame(Game game){
    this.game = game;
    this.pk.setGameName(this.game.getName());
  }

@ManyToOne
  public Player getPlayer() { return player;}
  public void setPlayer(Player player) { this.player = player; }
}
[Message sent by forum member 'gyorke' (gyorke)]

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