0

I am trying to implement Composite identifiers with @EmbeddedId. On running the below main method dummy1 table got data inserted. There were no records inserted inside dummy3 table. What changes are required to insert data in dummy3 table?. Please find the below classes for your reference.

Dummy1.java

@Entity(name="Dummy1")
public class Dummy1 {

    private String name;

    @EmbeddedId
    private Dummy2 dummy2;

     //Getters and setters are omitted for brevity
}

Dummy2.java

@Embeddable
public class Dummy2 implements Serializable {

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="id")
    private Dummy3 d3;

    @Column(name="State")
    private String state;

    @Column(name="city")
    private String city;
//Getters and setters are omitted for brevity

}

Dummy3.java

@Entity(name="Dummy3")
public class Dummy3 {

    @Id
    private int id;

    private String name;

    private String lastName;

//Getters and setters are omitted for brevity
}

Main.java

public static void main(String[] args) {
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure("/resources/hibernate.cfg.xml")
                .build();

        factory = new MetadataSources(registry)
                .buildMetadata()
                .buildSessionFactory();

        session=factory.openSession();


        session.getTransaction().begin();

        Dummy1 a=new Dummy1();
        a.setName("aaaa");

        Dummy2 b=new Dummy2();
        b.setCity("aaa");
        b.setState("bbb");

        Dummy3 c=new Dummy3();
        c.setId(1);
        c.setName("ddd");
        c.setLastName("dddd");

        b.setD3(c);
        a.setDummy2(b);

        session.persist(a);
        session.getTransaction().commit();
        session.close();

    }
K.Nicholas
  • 10,956
  • 4
  • 46
  • 66
Chirag
  • 211
  • 4
  • 16
  • What is your use case? What are you trying to do by creating a primary key that has a foreign key? – K.Nicholas Jun 27 '18 at 18:30
  • @K.Nicholas http://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#identifiers-composite-aggregated I am trying to implement example 149 – Chirag Jun 27 '18 at 18:33
  • You should persist the Dummy3 entities. – K.Nicholas Jun 27 '18 at 18:44
  • Possible duplicate of [JPA @ManyToOne with CascadeType.ALL](https://stackoverflow.com/questions/13027214/jpa-manytoone-with-cascadetype-all) – K.Nicholas Jun 27 '18 at 18:46
  • I have added @ManyToOne(cascade=CascadeType.ALL) in dummy2. Wont that do the same? Is there any other way to persist it without explicitly persisting it? – Chirag Jun 27 '18 at 18:47
  • @K.Nicholas that's different. – Chirag Jun 27 '18 at 18:50
  • It's exactly your question. Take your time and look it over closely. Maybe not technically accurate but Cascade is for Parent to Child and Dummy2 is a Child of Dummy3 so cascade doesn't belong there and seems like it isn't working. It is something the example doesn't have as well. Better to look a little closer at how cascade works if interested. – K.Nicholas Jun 27 '18 at 18:53
  • @K.Nicholas If I move the ManyToOne(cascade=CascadeType.ALL) in dummy2 to dummy1 with corresponding setters and getters It works but my requirement is too persist them through Embeddable class i.e. Dummy2 – Chirag Jun 28 '18 at 06:11
  • Well, it's not part of the original example and I can think of no sensible reason to use cascade that way so good luck but I don't think you'll get much help with it. – K.Nicholas Jun 28 '18 at 07:25

0 Answers0