![]() In this section, you will learn how to do one to one mapping in Hibernate Session session = sessionFactory.Hibernate 4 One to One Mapping using Annotation SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry) builds a session factory from the service registry ServiceRegistry serviceRegistry = registry.buildServiceRegistry() Registry.applySettings(configuration.getProperties()) ServiceRegistryBuilder registry = new ServiceRegistryBuilder() * in order to implement a one-to-many association mapping.Ĭonfiguration configuration = new Configuration().configure() * This program demonstrates using JPA annotations in Hibernate Now, let’s write a test program ( StockManager.java) with the following source code: package Writing a Test ProgramSo far we have finished the configuration part. Therefore, remember the key annotations are used for mapping a one-to-many association: and Writing Hibernate Configuration FileConfigure database connection settings and mapping classes in the file as follows: ĥ. }Here, the annotation specifies the join column of the association. ![]() In the Product side: private Category = "CATEGORY_ID"). ![]() }Here, the mappedBy attribute is mandatory, as it specifies that the one-to-many association is mapped by this side ( Category) and the cascade attribute make sure Hibernate will save/update the products set when saving/updating this category. In the Category side: private Set = "category", cascade = CascadeType.ALL).We pay attention to the annotations that make the one-to-many association: and Let’s look at the code closely: They are the basic elements needed for annotating any entities in Hibernate, regardless of the association. The and annotations are self-explanatory and easy to understand. }Note that, the Category class has a set of Products, and the Product has a reference back to the Category.And, as we can see, only JPA annotations are used to annotate the model classes. Public Product(String name, String description, float price, }File net\codejava\hibernate\Product.java: package Return = "category", cascade = CascadeType.ALL) Annotating Model Classes for One-to-Many AssociationCreate two model classes called Category.java and Product.java with the following source code:File net\codejava\hibernate\Category.java: package For JAR files of Hibernate and MySQL Connector/J, update the pom.xml file as follows: Įxample of a Hibernate one-to-many association mappingĪnd Maven will resolve related dependencies if any.ģ. Note that when using annotations approach, the XML mapping files for the model classes are no longer needed. Model classes: Category.java and Product.java.Creating Maven-Eclipse ProjectIn Eclipse IDE, create a Maven project named HibernateOne2ManyAnnotationsExample with the following structure: This project consists of the following files: The structure of the stockdb database would look like this:Ģ. `product_id` int(11) NOT NULL AUTO_INCREMENT,ĬONSTRAINT `fk_category` FOREIGN KEY (`category_id`) REFERENCES `category` (`category_id`) ![]() `category_id` int(11) NOT NULL AUTO_INCREMENT, Creating Database and TablesExecute the following MySQL script to create a database stockdb and two tables category and product: create database stockdb
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |