0

I am new to JPA and have a question concerning naming.

I recently had some trouble where I got errors saying something like that there where two entities with the same name in my persistence unit. (I was stupid and did not save the error message)

I am making a web application (Java EE 7, Glassfish) which connects to a remote db using an API (UniProt JAPI). The API classes seem to contain an entity named 'Protein'. The thing is I have a local db which I wanted to write some entity classes for, one of which was called 'Protein' because the table in my local db was called 'protein'. This seemed to cause naming conflicts with the externaly derived 'Protein' entity. The easiest way around this I figured was to rename my local protein entity.

But I'm unsure of how to do this.

My guess was doing something like this:

@Entity(name="MyProtein")
@Table(name="protein")
public Class MyProtein { 

Because I thaught that the name="MyProtein" in the @Entity annotation will set the entity name. Is this so and does this have to be the same as the class name?

And the name="protein" in the @Table annotation I figured set from which table to map the entity. Is this so and does that mean that an entity can only map to one table in the database? Does the table annotation have any relation to the class name?

numfar
  • 1,637
  • 4
  • 18
  • 39

2 Answers2

0

@Entity is fine. You don't need to use name inside that.

Using @Table entity you are mapping the table with your entity through the name value.

For a JPA project you can have one entity to point only a single table. And, logically multiple entities can point to a single table(I have never used such things).

Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
0

In given example name attribute of @Entity makes no difference. Name defaults to the unqualified name of entity class which is this case same. This attribute comes handy when there is two entity classes with identical names in different packages:

package a;

@Entity
public class MyProtein { ... }


package b;
//for this one we override default name (MyProtein)
@Entity (name = "SomethingElse")
public class MyProtein { ... }

It was needed to rename second entity, because we cannot have multiple entities with same name.

Name of the table defaults to the name of the entity.

  • Entity name for a.MyProtein is MyProtein (default), so is the name of database table.
  • Entity name for b.MyProtein is SomethingElse, so is the name of the database table

When it is needed to have other table name than default one, @Table can be used. API documentation explain attributes and their defaults.

Persistent attributes of entity can be divided to multiple tables, but each entity have only one primary table and one attribute is stored only to the one table. Secondary table is specified with @SecondaryTable. When there is many of them, then @SecondaryTables comes to picture. If attribute should be stored to secondary table, name of table is given in name attribute of @Column

Maybe this comes more clear via example:

//overrides default MyProtein
@Entity(name="SomeEntity")
//overrides default SomeEntity (default is entity name, not the name of class)
@Table(name="protein")
@SecondaryTables({
    @SecondaryTable(name="more_protein_attributes"),
    @SecondaryTable(name="and_even_more")
})
public class MyProtein {
   @Id private Integer id;
   //attribute to be stored to to 'protein' table
   private String attr1;

   @Column(table = "more_protein_attributes")
   private String attr2;

   @Column(table = "and_even_more")
   private String attr3;

}
Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135