Commit 47a3a560 authored by Suresh Kumar's avatar Suresh Kumar

Hibernate

parent 6c965654
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hibernatemanytomanyrelationship</groupId>
<artifactId>RelationShipBetweenStudentsAndCourses</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>RelationShipBetweenStudentsAndCourses</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.5.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.hibernatemanytomanyrelationship;
import java.util.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class App
{
public static void main( String[] args )
{
System.out.println( "Project Started....!" );
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Students s1 = new Students();
Students s2 = new Students();
s1.setSid(5);
s1.setRno("102");
s1.setName("M_Asad Iqbal");
s2.setSid(6);
s2.setRno("106");
s2.setName("Arsam Ali");
Courses c1 = new Courses();
Courses c2 = new Courses();
c1.setCid(5);
c1.setCoursename("Java");
c2.setCid(6);
c2.setCoursename("JavaScript");
List<Students> list1 = new ArrayList<Students>();
List<Courses> list2 = new ArrayList<Courses>();
list1.add(s1);
list1.add(s2);
list2.add(c1);
list2.add(c2);
s1.setCourses(list2);
c2.setStudents(list1);
Session s = factory.openSession();
Transaction txt = s.beginTransaction();
s.save(s1);
s.save(s2);
s.save(c1);
s.save(c2);
txt.commit();
s.close();
}
}
package com.hibernatemanytomanyrelationship;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@Entity
public class Courses {
@Id
private int cid;
@Column(name="course_name")
private String coursename;
@ManyToMany(mappedBy ="courses")
private List<Students> students;
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCoursename() {
return coursename;
}
public void setCoursename(String coursename) {
this.coursename = coursename;
}
public List<Students> getStudents() {
return students;
}
public void setStudents(List<Students> students) {
this.students = students;
}
public Courses(int cid, String coursename, List<Students> students) {
super();
this.cid = cid;
this.coursename = coursename;
this.students = students;
}
public Courses() {
super();
// TODO Auto-generated constructor stub
}
}
package com.hibernatemanytomanyrelationship;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@Entity
public class Students {
@Id
private int sid;
private String rno;
private String name;
@ManyToMany
private List<Courses> courses ;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getRno() {
return rno;
}
public void setRno(String rno) {
this.rno = rno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Courses> getCourses() {
return courses;
}
public void setCourses(List<Courses> courses) {
this.courses = courses;
}
public Students(int sid, String rno, String name, List<Courses> courses) {
super();
this.sid = sid;
this.rno = rno;
this.name = name;
this.courses = courses;
}
public Students() {
super();
// TODO Auto-generated constructor stub
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/myhibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">nisum123</property>
<property name="dialect">org.hibernate.dialect.MySQL55Dialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping class="com.hibernatemanytomanyrelationship.Students" />
<mapping class="com.hibernatemanytomanyrelationship.Courses" />
</session-factory>
</hibernate-configuration>
\ No newline at end of file
package com.hibernatemanytomanyrelationship;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment