0 Replies Latest reply on Jan 23, 2006 7:40 PM by Jeremy Norris

    Attaching a new, unmanaged instance:

    Jeremy Norris Newbie

      Consider the following example:

      @Entity
      @Table
      (
      name="foobar",
      uniqueConstraints={@UniqueConstraint(columnNames={"natural_key"})}
      )
      class Foobar
      {
      @Id(generate = GeneratorType.AUTO)
      @Column(name = "primary_key")
      long primary_key;

      @Column(name = "natural_key"
      String natural_key;

      public Foobar(String natural_key)
      {
      this.natural_key = natural_key;
      }

      equals() // use natural key ...
      hashcode() // use natural key ...
      }

      -----

      I would like to be able to instantiate a new instance via the natural key constructor and persist this with merge()-like semantics. By this I mean I want the entity manager to determine whether persisting the new instance will result in an insert or an update based on the unique constraint annotations on the entity. I cannot use merge() for this since the instance will not be detached, rather it will be new (without the db generated PK set). I could do this manually by coding a find then insert/update myself, but it seems like something the persistence layer could do for me. Furthermore if I do it myself, then I am duplicating the unique constraint information for this class (in the annnotation and in my find() method).

      Any ideas/suggestions? Thanks.