How to map a table without ID, using Hibernate annnotaions?

Asked

Viewed 735 times

0

Hello, good morning, sir. Having a table that relates USER and ACHIEVEMENTS called USER_ACHIEVEMENTS, where it does not have ID, only USER_ID and ACHIEVEMENTS_ID, ie a manyToMany relationship.

How to map this table in Hibernate, without it having an ID?

Follow my class Entity:

@Entity
@Table(name="USER_ACHIEVEMENTS")
public class UserAchievements {

@Column(name="USER_ID", nullable=false)
private int userId;

@Column(name="ACHIEVEMENTS_ID", nullable=false)
private int achievementsId;

When running the project, with this class mapped this way, the error below happens:

Caused by: org.hibernate.Annotationexception: No Identifier specified for Entity: com.poli.server.PoliServer.Model.Userachievements

  • You have two ways to map this kind of relationship, you can create an associative entity, which is how you’re doing it or you can just map from within the entities .

1 answer

0


To create a Many to Many mapping you can map your entities in this way, without any need to transform your associative table into a java object.

@Entity
@Table(name="notebook")
public class Notebook {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    private String serialNumber;
    private int ramMemoryTotal;
    private int hdSpaceTotal;

    //Relacionamento implementado
    @ManyToMany(mappedBy="notebooks")
    private List pessoas
}

In Person I do the following:

@Entity
@Table(name = "Pessoa")
@SecondaryTable(name = "health_care", pkJoinColumns = 
{ @PrimaryKeyJoinColumn(name = "id") })
public class Pessoa {

    @Id
    private int id;

    @Column
    private String name;

    @Column(table = "health_care", name = "company_name")
    private String companyName;

    @ManyToMany
    @JoinTable(name="pessoa_has_notebooks", joinColumns=
    {@JoinColumn(name="pessoa_id")}, inverseJoinColumns=
      {@JoinColumn(name="notebook_id")})
    private List notebooks;
  }
  • Hi, good afternoon. I also use this way, but in my specific case, I need to map the relationship table. But thank you very much. I’m going to do it that way until I find the way I was thinking. Thank you

  • In this case, you can map a composite key, because @Entity forces you to identify an ID, either with@Id or@Embeddedid.'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.