2

Sorry if my post is duplicated or the tittle doesn't describe the topics, because I don't know how to describe this in the tittle, I look on internet, but I didn't find the solution.

I am using Java and JPA. The problem is the next :

I have a class A with an autogenerated key :

class A{
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private int id;
      private List<B> listB;
}

And the class B with the id of this clas:

 class B {
         @EmbeddedId
         private Bid id;
         private String att;
     }
     class Bid {
         private int idA;
         private String text;
     }

In a controller I want to create an object A, the problem is when I created the object A, I need to create the object B where the id of B contains the id of A which is autogenerated, and it is created in the moment when the entity is mapped to de database, I dont't know how to set the id autogenerated of A into the idB, maybe I should query to de database asking what is the las id of classA, but it seem bad.

Thanks in advance

2 Answers2

1

Your case is a derived identifier case, where your entity B's identity was derived from the primary key of A. You can use @MapsId annotation for this case and your entities can be restructured like this:

@Entity
public class A {

  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private int id;

  @OneToMany(mappedBy="a")
  private List<B> listB = new ArrayList<B>();
  ...
}

@Entity
public class B {
    @EmbeddedId
    private BId id;

    @ManyToOne
    @MapsId("idA")
    private A a;
    ...
}

@Embeddable
public class BId {
    private int idA;
    private String att;
    ...
}

This is how you would persist the entities:

A a = new A();

BId bid = new BId();
bid.setAtt("text"); // notice that the idA attribute is never manually set, since it is derived from A

B b = new B();
b.setId(bid);
b.setA(a);

a.getListB().add(b);

em.persist(a);
em.persist(b);

See sample implementation here.

Ish
  • 3,992
  • 1
  • 17
  • 23
  • I tried with this, but throw an error " javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: null id generated for:ClassB" –  Oct 09 '15 at 19:08
  • To be honest, I'm not sure how you're getting this error. It might help if you could post the code where you try to persist both entities (A and B). I also tried setting up on my end using the code I posted here, both with Oracle (using sequence generator) and MySQL (using identity generator). Both are working perfectly fine. If you want you can download the project here (it's JPA Hibernate Eclipse Maven project): https://drive.google.com/file/d/0B4STeDDZx9s_X01RMFIzVzNvYU0/view?usp=sharing – Ish Oct 09 '15 at 22:00
  • the unique diference is that Class A has a list of b, and I iterate with the list of b in a and doing em.persist(b_i) –  Oct 09 '15 at 22:49
  • Please update your post and show your code in persisting both entities - A and list of Bs, so we can inspect what's the problem. – Ish Oct 10 '15 at 00:17
  • Thanks Ish, it Works well! The problem was I was setting wrong –  Oct 10 '15 at 22:55
-1

It would be useful to know which is the case scenario you are trying to solve in general because the structure you are using seems unnecessarily complex. What is your real goal?

gmcontessa
  • 558
  • 4
  • 10
  • I want to do $B = new B()$ and then create a list of A and set into the B –  Oct 09 '15 at 18:24