Can you really produce a table (from the JPA annotated hibernate entity) that doesn't have a primary key / Id?

(I understand this really is not recommended. A table must have a primay key.)

Roger's self-response is correct. To elaborate a little on which is intended (I wasn't obvious onto it in the beginning and figured this could help):

If you have you've got a table Foo as a result:

TABLE Foo (
bar varchar(20),
bat varchar(20)
)

Normally, you are able to write a category w/Annotations to utilize this table:

// Technically, for this example, the @Table and @Column annotations 
// are not needed, but don't hurt. Use them if your column names 
// are different than the variable names.

@Entity
@Table(name = "FOO")
class Foo {

  private String bar;
  private String bat;


  @Column(name = "bar")
  public String getBar() {
   return bar;    
  }

  public void setBar(String bar) {
   this.bar = bar;    
  }

  @Column(name = "bat")
  public String getBat() {
   return bat;    
  }

  public void setBat(String bat) {
   this.bat = bat;    
  }

}

.. But, darn. This table is not we are able to use being an id, and it is a legacy database that people use for [place vital business function]. I do not think they'll allow me to start modifying tables to ensure that me to make use of hibernate.

You are able to, rather, split the item up right into a hibernate-workable structure which enables the whole row for use because the key. (Naturally, this assumes the row is exclusive.)

Split the Foo object into two thusly:

@Entity
@Table(name = "FOO")
class Foo {

  @Id
  private FooKey id;

  public void setId(FooKey id) {
    this.id = id;
  }

  public void getId() {
    return id;
  }
}

and

@Embeddable
class FooKey implements Serializable {
  private String bar;
  private String bat;

  @Column(name = "bar")
  public String getBar() {
   return bar;    
  }

  public void setBar(String bar) {
   this.bar = bar;    
  }

  @Column(name = "bat")
  public String getBat() {
   return bat;    
  }

  public void setBat(String bat) {
   this.bat = bat;    
  }

.. Which ought to be it. Hibernate uses the Embeddable key because of its needed identity and you will create a call normally:

Query fooQuery = getSession().createQuery("from Foo");

Hope this can help first-timers with getting this working.

I discovered that it is difficult to do this. So misfortune for individuals dealing with legacy systems. Should you reverse engineer (create jpa annotated organizations from existing jdbc connection) the table can create two java classes, one Entity with one area id, and something embeddable id containg all of the posts out of your relation.

well it is possible or otherwise to make use of hibernate with tables without primary key, anyway I believe that tables should have primary key

Use following code Hibernate does not have its very own logic to tell apart duplicate records

Tell me if you will find any difficulties with this method

@Entity @IdClass(Foo.class)
class Foo implements Serializable {
  @Id private String bar;
  @Id private String bat;

  public String getBar() {
   return bar;    
  }

  public void setBar(String bar) {
   this.bar = bar;
  }


  public String getBat() {
   return bat;    
  }

  public void setBat(String bat) {
   this.bat = bat;    
  }
}

Whether it appears like a seafood, swims in water, eats seafood food and has the aroma of a seafood is most likely is really a seafood. Re-architect you application and employ a principal key correctly.

ps. Hibernate, JPA requires each entity with an ID (primary key)

You don't have to produce a separate class to become your @Id or Primary Key. Only use an Integer (or whatever). Also, don't publish the fake key as designers who utilize it may think it's real and otherwise use it. Lastly, this really is best utilized in a VIEW. To be sure with earlier posts that in many, if not completely cases, tables must have a principal key. For instance:

@Entity
@Table(name = "FOO")
class Foo {

  @SuppressWarnings("unused")
  @Id
  private Integer id;

  @Column(name = "REAL_COLUMN")
  private String realColumn;

  public String getRealColumn() {
    return realColumn;    
  }

  public void setRealColumn(String realColumn) {
    this.realColumn= realColumn;    
  }


}