Thursday 15 September 2011

java - Hibernate One-to-many mapping -


I'm new to hibernate, trying to know many relationships from one, executing the code without any errors But it does not only create tables, but to put a value on it, I want to establish a number of relations between the employee and his assigned work. An employee can have many tasks.

Staff. Java

@Entity Public Square Employee {@Id @ generatedValue (strategy = GenerationType.ENTITYITY) Private Int ID; The name of the private string; @OneToMany private set & lt; Tasks & gt; Taklist = new hashset & lt; Tasks & gt; (); Public int getId () {return ID; } Public Zero setId (int id) {this.id = id; } Public string getName () {return name; } Public Zero Setname (string name) {this.name = name; } Public set & lt; Work & gt; GetTaklist () {Return taklist; } Public Zero setTaklist (set & lt; task & gt; taklist) {this.taklist = taklist; }}

Task. Java

  @Active Public Squared Work {@Id @ generatedValue (strategy = GenerationType.IDENTITY) Private Ent Tasked; Private string function name; Public int getTaskid () {returnid Cupid; } Public Zero Satcidid (Int Tasked) {this.taskid = taskid; } Public string getTaskname () {return taskname; } Public Zero Set Taskname (string function name) {this.taskname = taskname; }}  

hibernate.cfg.xml

& lt; Hibernate-configuration & gt; & Lt; Session-Factory & gt; & Lt; Property Name = "hibernate.dialect" & gt; Org.hibernate.dialect.MySQLDialect & lt; / Property & gt; & Lt; Property Name = "hibernate.connection.driver_class" & gt; Com.mysql.jdbc.Driver & lt; / Property & gt; & Lt ;! - Assume that the name of the test database is - & gt; & Lt; Property name = "hibernate.conction.value" & gt; Jdbc: mysql: // localhost / hibernate & lt; / Property & gt; & Lt; Property Name = "hibernate.connection.username" & gt; Root & lt; / Property & gt; & Lt; Property Name = "hibernate.connection.password" & gt; Root & lt; / Property & gt; & Lt; Property Name = "connection.pool_size" & gt; 10 & lt; / Property & gt; & Lt; Property Name = "show_sql" & gt; True & lt; / Property & gt; & Lt; Property Name = "hibernate.hbm2ddl.auto" & gt; Create & lt; / Property & gt; & Lt; Mapping class = "com.sumeet.onetomany.Employee" & gt; & Lt; / Mapping & gt; & Lt; Mapping class = "com.sumeet.onetomany.Task" & gt; & Lt; / Mapping & gt; & Lt; / Session-Factory & gt; & Lt; / Hibernate-configuration & gt;

App.Java

  package com.sumeet.onetomany; Import org.hibernate.Session; Import org.hibernate.SessionFactory; Import org.hibernate.cfg.configuration; Public class app {public static zero main (string agr []) (session factory factory = new configuration.) Configure (). BuildSessionFactory (); Session session = factory. Open session (); Employee employee = new employee () ; Emp.setName ("Arun"); Task Task = New Task (); Task.setTaskname ("Office Tasks"); Task task2 = New Task (); Task2.setTaskname ("Home Work"); Emp.getTaklist (); Session.beginTransaction (); session.save (EMP); session.close ();}}  

you Need to do the transaction

  transaction requires tx = session.beginTransaction (); session.save (emp); tx.commit (); // transaction session.close () ;  

No comments:

Post a Comment