Java

Java is a set of computer software and specifications developed by Sun Microsystems, which was later acquired by the Oracle Corporation, that provides a system for developing application software and deploying it in a cross-platform computing environment. Java is used in a wide variety of computing platforms from embedded devices and mobile phones to enterprise servers and supercomputers.

Spring Logo

Spring Framework

The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key element of Spring is infrastructural support at the application level: Spring focuses on the "plumbing" of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments.

Hibernate Logo

Hibernate Framework

Hibernate ORM is an object-relational mapping framework for the Java language. It provides a framework for mapping an object-oriented domain model to a relational database.

Sunday, May 17, 2015

What are all the different ways to create an object in Java?

There are four ways to create a Object in Java. They are

1. Using new keyword - This is the most common way to create an object in java.
Example:
MyObject object = new MyObject();

2. Using Class.forName() - If we know the name of the class and if it has a public default constructor we can create an object in this way.
Example:
MyObject object = (MyObject) Class.forName("com.ranga.MyObject").newInstance();

3. Using clone() - The clone() can be used to create a copy of an existing object.
Example:
MyObject anotherObject = new MyObject();
MyObject object = (MyObject) anotherObject.clone();

4. Using object deserialization - Object deserialization is nothing but creating an object from its serialized form.
Example:
ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();

Write a Program to Implement HashTable in Java Script

<!DOCTYPE html>
<html>
<body>
<script>

function HashTable() {
var hashTableItems = {};
this.setItem = function(key, value) {
hashTableItems[key] = value;
}
this.getItem = function(key) {
return hashTableItems[key];
}
}

var hashTable = new HashTable();
hashTable.setItem('name', 'Ranga Reddy');
console.log(hashTable.getItem('name'));

var hashTable1 = new HashTable();
hashTable1.setItem('age', '27');
console.log(hashTable1.getItem('age'));
</script>
</body>
</html>

Writing our own Collection mechanism to Sort different objects in Java.

SortCollections.java
---------------------------------------
package com.ranga.collections;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortCollections<T> {

private List<T> collectionList;
private String sortingField = null;
private boolean isAscending = true;

public SortCollections(List<T> objectList, String sortField) {
this(objectList, sortField, true);
}

public SortCollections(List<T> collectionList, String sortField, boolean isAscending) {
super();
this.collectionList = collectionList;
this.sortingField = sortField;
this.isAscending = isAscending;
}

@SuppressWarnings("rawtypes")
public List<T> sort( final Class claz) {
final String sortingField = this.sortingField;
final Boolean isAscending = this.isAscending;

Collections.sort(this.collectionList, new Comparator<T>() {
@Override
public int compare(T object1, T object2) {
try {
Field sortField = claz.getDeclaredField(sortingField);
sortField.setAccessible(true);

Object value1 = sortField.get(object1);
Object value2 = sortField.get(object2);

if (value1 instanceof String && value2 instanceof String) { // String field
String string1 = (String) value1;
String string2 = (String) value2;
if (true == isAscending) {
return string1.compareTo(string2);
} else {
return string2.compareTo(string1);
}
} else { // Numeric field
Number number1 = (Number) value1;
Number number2 = (Number) value2;
if (true == isAscending) {
if (number1.floatValue() > number2.floatValue()) {
return 1;
} else {
return -1;
}
} else {
if (number2.floatValue() > number1.floatValue()) {
return 1;
} else {
return -1;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
});
return this.collectionList;
}
}

Employee.java
---------------------------------------
    package com.ranga.collections;
import java.io.Serializable;
public class Employee implements Serializable {

private int id;
private String name;
private int age;
private float salary;

public Employee() {
super();
}

public Employee(int id, String name, int age, float salary) {
super();
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public float getSalary() {
return salary;
}

public void setSalary(float salary) {
this.salary = salary;
}

@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age
+ ", salary=" + salary + "]";
}
}

Application.java
---------------------------------------
package com.ranga.collections;
import java.util.ArrayList;
import java.util.List;
public class Application {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee(1,"Ranga", 26, 25677 ));
employees.add(new Employee(2,"Raja", 50, 2577 ));
employees.add(new Employee(3,"Vasu", 20, 30677 ));
employees.add(new Employee(4,"Mani", 45, 45677 ));
employees.add(new Employee(5,"Yasu", 29, 67677 ));
employees.add(new Employee(6,"Vinod", 80, 5677 ));

System.out.println(employees);
List<Employee> employeeList = new SortCollections<Employee>(employees, "name", false).sort(Employee.class);
System.out.println(employeeList);

employeeList = new SortCollections<Employee>(employees, "salary").sort(Employee.class);
System.out.println(employeeList);
}
}

Write a SQL Program to Swap the values in a single update query.

Given a Student table, Swap all Male to Female value and vice versa with a single update query.

Id Name Sex Salary
----------------------------
1 Ranga Male 2500
2 Vasu Female 1500
3 Raja Male 5500
4 Amma Female 500

Program:
------------------------
UPDATE Gender SET sex = CASE sex WHEN 'Male' THEN 'Female' ELSE 'Male' END

Write a SQL Program to Select every Nth record.

CREATE TABLE student(id int, name varchar(30), age int, gender char(6));

INSERT INTO student VALUES
(1 ,'Ranga', 27, 'Male'),
(2 ,'Reddy', 26, 'Male'),
(3 ,'Vasu', 50, 'Female'),
(4 ,'Ranga', 27, 'Male'),
(5 ,'Raja', 10, 'Male'),
(6 ,'Pavi', 52, 'Female'),
(7 ,'Vinod', 27, 'Male'),
(8 ,'Vasu', 50, 'Female'),
(9 ,'Ranga', 27, 'Male'),
(10 ,null, 27, 'Male');

Program:
-----------------------------------------------
SELECT * FROM (
SELECT @row := @row +1 AS Rownum, name as Name FROM (SELECT @row :=0) r, student
) students
WHERE rownum %3 = 1;

Output:
-----------------------------------------------
Rownum Name
1 Ranga
4 Ranga
7 Vinod
10 (null)

Write a SQL Program to get the Sum value of same column with different conditions.

CREATE TABLE student(id int, name varchar(30), age int, gender char(6));

INSERT INTO student VALUES
(1 ,'Ranga', 27, 'Male'),
(2 ,'Reddy', 26, 'Male'),
(3 ,'Vasu', 50, 'Female'),
(5 ,'Raja', 10, 'Male'),
(6 ,'Pavi', 52, 'Female'),
(7 ,'Vinod', 27, 'Male');

Query:
-----------------------------------
SELECT SUM(CASE WHEN s.gender = 'Male' THEN 1 ELSE 0 END) AS MaleCount,
SUM(CASE WHEN s.gender = 'Female' THEN 1 ELSE 0 END) AS FemaleCount
FROM
student s;

Output:
------------------------------
MaleCount FemaleCount
4 2

Write a SQL Program to Get the Next and Previous values based on Current value?

CREATE TABLE student (id int, name varchar(30), age int, gender char(6));

INSERT INTO student VALUES
(1 ,'Ranga', 27, 'Male'),
(2 ,'Reddy', 26, 'Male'),
(3 ,'Vasu', 50, 'Female'),
(4 ,'Ranga', 27, 'Male'),
(5 ,'Raja', 10, 'Male'),
(6 ,'Pavi', 52, 'Female'),
(7 ,'Vinod', 27, 'Male'),
(8 ,'Vasu', 50, 'Female'),
(9 ,'Ranga', 27, 'Male');

Query:
-----------------------------------
SELECT name as Name,
(SELECT name FROM student s1
WHERE s1.id < s.id
ORDER BY id DESC LIMIT 1) as Previous_Name,
(SELECT name FROM student s2
WHERE s2.id > s.id
ORDER BY id ASC LIMIT 1) as Next_Name
FROM student s
WHERE id = 7;

Output:
------------------------------------------------------
Name Previous_Name Next_Name
Vinod Pavi Vasu

How to get the Duplicate and Unique Records by using SQL Query?

CREATE TABLE student (id int, name varchar(30), age int, gender char(6));

INSERT INTO student VALUES
(1 ,'Ranga', 27, 'Male'),
(2 ,'Reddy', 26, 'Male'),
(3 ,'Vasu', 50, 'Female'),
(4 ,'Ranga', 27, 'Male'),
(5 ,'Raja', 10, 'Male'),
(6 ,'Pavi', 52, 'Female'),
(7 ,'Vinod', 27, 'Male'),
(8 ,'Vasu', 50, 'Female'),
(9 ,'Ranga', 27, 'Male');

Getting the duplicate records:
---------------------------------------
SELECT DISTINCT name AS Name, COUNT(name) as Count FROM student GROUP BY name HAVING COUNT(name) > 1;

Output:
---------------------------------------
Name Count
Ranga 3
Vasu 2

Getting the Unique records:
---------------------------------------
SELECT DISTINCT name AS Name FROM student GROUP BY name;

Output:
---------------------------------------
Name
Pavi
Raja
Ranga
Reddy
Vasu
Vinod

How to delete the Child records while updating Parent Record.

If we use javax.persistence.CascadeType.All for deleting child records it will delete only collection of strings. Other than collection of strings in order to delete we need to use
org.hibernate.annotations.CascadeType.DELETE_ORPHAN to delete the child records.
For Example,
public class Employee {    
            @OneToMany(cascade = {CascadeType.ALL})
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
List<Department> departments;
}

What is the output of the following SQL Program. SELECT CASE WHEN null = null THEN 'I LOVE YOU RANGA' ELSE 'I HATE YOU RANGA' end as Message;

SELECT CASE WHEN null = null THEN 'I LOVE YOU RANGA' ELSE 'I HATE YOU RANGA' end as Message;
Output: 'I HATE YOU RANGA'
The reason for this is that the proper way to compare a value to null in SQL is with the is operator, not with =.
SELECT CASE WHEN null IS null THEN 'I LOVE YOU RANGA' ELSE 'I HATE YOU RANGA' end as Message;
Output: 'I LOVE YOU RANGA'

Write a SQL Program to generate the following output?

Input:
Employee:
Department:
Output:
Creating tables and inserting Data:
CREATE TABLE Employee (
e_id INT NOT NULL AUTO_INCREMENT,
e_name VARCHAR(100) NOT NULL,
age tinyint NOT NULL,
PRIMARY KEY (e_id)
);

CREATE TABLE Department (
d_id INT NOT NULL AUTO_INCREMENT,
d_name VARCHAR(100) NOT NULL,
e_id INT NOT NULL,
PRIMARY KEY (d_id),
FOREIGN KEY (e_id)
REFERENCES Employee(e_id)
ON DELETE CASCADE
);

INSERT INTO Employee VALUES (1,'Ranga', 27), (2, 'Raja', 50), (3, 'Vasu',45) ,
(4, 'Vinod', 27), (5, 'Manoj',27);

INSERT INTO Department VALUES (1,'HR', 2), (2, 'Finance', 4), (3, 'Software',1) ,
(4, 'Finance', 3), (5, 'Hardware',1), (6, 'Software', 5), (7, 'Finance', 1);
Query: 
SELECT group_concat(e.e_name) as Employee_Names, d.d_name as Department_Name FROM Employee e INNER JOIN Department d ON d.e_id = e.e_id GROUP BY d.d_name;
Happy Coding!!!

Sunday, May 3, 2015

Hibernate4 One-to-One Relationship Example Using Annotations

In this article we are going to create a project to implement step by step one-to-one association example using Annotations with Maven project.

OneToOne Relationship:
A one-to-one relationship occurs when one entity is related to exactly one occurrence in another entity. For example, we have a Employee and Address tables, Employee has its single Address and each Address belongs to unique Employee. 

We can associate entities through a one-to-one relationship using the @OneToOne annotation (javax.persistence.OneToOne).

There are three different ways to implement OneToOne Association. 
1. Either the associated entities share the same primary key values(Shared Primary Key association).
2. A foreign key is held by one of the entities (One-to-One foreign key association).
3. A association table is used to store the link between the 2 entities (Using a join table).

1) One-to-One association using shared primary keys:
A one to one shared primary key relationship would mean that a Employee table does not have a foreign key column to Address table, rather the Address table's primary key value is the same as Employee, and acts as a foreign key to the Employee table.

In order to use a one to one with a shared primary key, ids on both sides of the object need a @GeneratedValue. The owner can use a generic generator where it gets a fresh number every time, but the other side of the one to one needs a custom hibernate extension, which is from @GenericGenerator. In our example, Address will have the hibernate extension @GenericGenerator.
Employee.java

@Entity
@Table(name="Employee")
public class Employee implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="employeeId")
private long id;

@OneToOne(mappedBy="employee", cascade=CascadeType.ALL, fetch = FetchType.LAZY)
private Address address;
    // some more properties
 
    public Employee() {
super();
}
// setters and getters
}
Address.java

@Entity
@Table(name="Address")
@GenericGenerator(name="employee-primarykey", strategy="foreign", parameters=@Parameter(name="property", value="employee"))
public class Address implements Serializable {
@Id
@Column(name="addressId")
@GeneratedValue(generator="employee-primarykey")
private long addressId;

// some other properties

@OneToOne
@PrimaryKeyJoinColumn
private Employee employee;

public Address() {
super();
}
// setters and getters
}

The @PrimaryKeyJoinColumn annotation does say that the primary key of the entity is used as the foreign key value to the associated entity.

Let us see the full example.

Tools and Technologies Used: 
1. Java 6 or Above Version 
2. Hibernate 4.3.7 Final 
3. MySQL5.6 
4. Maven 3.2.2 
5. Eclipse Luna
Project Structure: 
Step-1. Creating Database and Tables:
CREATE DATABASE `vara_softtech`;
use vara_softtech;

CREATE TABLE `employee` (
`employeeId` bigint(20) NOT NULL AUTO_INCREMENT,
`age` int(11) DEFAULT NULL,
`firstName` varchar(255) DEFAULT NULL,
`lastName` varchar(255) DEFAULT NULL,
PRIMARY KEY (`employeeId`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

CREATE TABLE `address` (
`addressId` bigint(20) NOT NULL,
`city` varchar(255) DEFAULT NULL,
`houseNo` varchar(255) DEFAULT NULL,
`street` varchar(255) DEFAULT NULL,
PRIMARY KEY (`addressId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Step-2. Creating  Maven-Eclipse Project:
In Eclipse IDE, create a Maven project named Hibernate4_Association_Mapping with the above structure.
This project consists of the following files:
Hibernate Configuration file: hibernate.cfg.xml
POJO classes: Employee.java and Address.java
Util class: HibernateUtil.java
Client Program: Application.java
Maven project: pom.xml
Step-3. Adding the project dependencies into pom.xml file:
<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.varasofttech</groupId>
<artifactId>Hibernate4_Association_Mapping</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Association Mapping</name>
<dependencies>

<!-- Hibernate framework -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.7.Final</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>

<!-- My SQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
</dependencies>
</project>

Step-4. Creating the Hibernate Configuration file hibernate.cfg.xml 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>

<!-- Database Settings -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///vara_softtech</property>
<property name="hibernate.connection.username">varasofttech</property>
<property name="hibernate.connection.password">varasofttech</property>

<!-- Dialect class -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

<!-- Hibernate specific -->
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>

<!-- OneToOne Mapping -->
<mapping class="com.varasofttech.onetoone.Address" />
<mapping class="com.varasofttech.onetoone.Employee" />
</session-factory>
</hibernate-configuration>
Step-5. Creating the Hibernate Helper class HibernateUtil.java:
package com.varasofttech.util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
/**
*
* @author Ranga Reddy
* @date Feb 24, 2015
* @version 1.0
* @description : HibernateUtil.java
*/

public class HibernateUtil {
private static SessionFactory sessionFactory = null;

public static SessionFactory getSessionFacoty() {
return buildSessionFactory();
}
private static SessionFactory buildSessionFactory() {
if (sessionFactory == null) {
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();

sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
return sessionFactory;
}
    public static void closeSessionFactory() {
if (sessionFactory != null)
sessionFactory.close();
            sessionFactory = null;
} }
Step-6. Creating the POJO classes Employee.java and Address.java:
package com.varasofttech.onetoone;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
* @author Ranga Reddy
* @date Apr 20, 2015
* @version 1.0
* @description : Employee.java
*/


@Entity
@Table(name="Employee")
public class Employee implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="employeeId")
private long id;
@Column
private String firstName;
@Column
private String lastName;
@Column
private int age;

@OneToOne(mappedBy="employee", cascade=CascadeType.ALL, fetch = FetchType.LAZY)
private Address address;

public Employee() {
super();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}

@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName
+ ", lastName=" + lastName + ", age=" + age + "]";
}
}
package com.varasofttech.onetoone;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.GenericGenerator;
/**
* @author Ranga Reddy
* @date Apr 20, 2015
* @version 1.0
* @description : Address.java
*/

@Entity
@Table(name="Address")
@GenericGenerator(name="employee-primarykey", strategy="foreign", parameters=@Parameter(name="property", value="employee"))
public class Address implements Serializable {

@Id
@Column(name="addressId")
@GeneratedValue(generator="employee-primarykey")
private long addressId;
@Column
private String houseNo;
@Column
private String street;
@Column
private String city;

@OneToOne
@PrimaryKeyJoinColumn
private Employee employee;

public Address() {
super();
}
public long getAddressId() {
return addressId;
}
public void setAddressId(long addressId) {
this.addressId = addressId;
}
public String getHouseNo() {
return houseNo;
}
public void setHouseNo(String houseNo) {
this.houseNo = houseNo;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}

public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
@Override
public String toString() {
return "Address [addressId=" + addressId + ", houseNo=" + houseNo
+ ", street=" + street + ", city=" + city + "]";
}
}
Step-7. Creating the Client Application Application.java:
package com.varasofttech.onetoone;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.varasofttech.util.HibernateUtil;

/**
* @author Ranga Reddy
* @date Apr 20, 2015
* @version 1.0
* @description : Application.java
*/

public class Application {
public static void main(String[] args) {
Employee employee = new Employee();
employee.setFirstName("Ranga");
employee.setLastName("Reddy");
employee.setAge(27);

Address address = new Address();
address.setCity("Bangalore");
address.setHouseNo("12345");
address.setStreet("SRI Nagar");

employee.setAddress(address);
address.setEmployee(employee);

SessionFactory sessionFactory = HibernateUtil.getSessionFacoty();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
long employeeId = (Long) session.save(employee);

transaction.commit();
session.close();

sessionFactory = HibernateUtil.getSessionFacoty();
session = sessionFactory.openSession();
Employee emp = (Employee) session.get(Employee.class, employeeId);
System.out.println("Employee Details :");
System.out.println(emp);

System.out.println("Employee Address Details :");
System.out.println(emp.getAddress());
session.close();
}
}
Step-8. Run the client application
Hibernate: insert into Employee (age, firstName, lastName) values (?, ?, ?)
Hibernate: insert into Address (city, houseNo, street, addressId) values (?, ?, ?, ?)
Hibernate: select employee0_.employeeId as employee1_1_0_, employee0_.age as age2_1_0_, employee0_.firstName as firstNam3_1_0_, employee0_.lastName as lastName4_1_0_ from Employee employee0_ where employee0_.employeeId=?
Hibernate: select address0_.addressId as addressI1_0_0_, address0_.city as city2_0_0_, address0_.houseNo as houseNo3_0_0_, address0_.street as street4_0_0_, employee1_.employeeId as employee1_1_1_, employee1_.age as age2_1_1_, employee1_.firstName as firstNam3_1_1_, employee1_.lastName as lastName4_1_1_ from Address address0_ left outer join Employee employee1_ on address0_.addressId=employee1_.employeeId where address0_.addressId=?
Employee Details :
Employee [id=1, firstName=Ranga, lastName=Reddy, age=27]
Employee Address Details :
Address [addressId=1, houseNo=12345, street=SRI Nagar, city=Bangalore]
One-to-One with a shared primary key saves a column in the database and usually forces a bidirectional relationship through an ORM such as hibernate.

Happy Coding!!!