How to implement one to many relationships with annotation?



How to implement one to many relationships with annotation?..

Answer / Sarika Singh

To implement a one-to-many relationship using annotations, you can use the `@OneToMany` and `@ManyToOne` annotations. Here's an example of a User entity having multiple Addresses: nn```javan@Entitynpublic class User {n @Idn private Long id;n @OneToMany(mappedBy = "user")n private List<Address> addresses;n}nn@Entitynpublic class Address {n @Idn private Long id;n @ManyToOnen @JoinColumn(name = "user_id")n private User user;n}n```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Hibernate Interview Questions

How do we create session factory in hibernate?

1 Answers  


How to enable logging (log4j) in hibernate?

1 Answers  


How many objects are there in hibernate architecture?

1 Answers  


Is hibernate lazy load by default?

1 Answers  


In which condition is one to many relationship used?

1 Answers  


What are the states of object in hibernate?

1 Answers  


How can we reduce database write action times in Hibernate?

1 Answers  


Is entity framework an orm?

1 Answers  


Write the code that maps the java object with the database tables?

1 Answers  


What are the important interfaces in hibernate?

1 Answers  


What is hibernate validator framework?

1 Answers  


How does hibernate useful in mapping object and its relations?

1 Answers  


Categories