Reputation: 311
I don't have yet any knowledge relating ORM, Hibernate, JPA and such, so is this a valid way of mapping associations between entities in database, without using them ?
Thanks.
User can register a company. Each company has many departments. Each department has many sectors, and so on with offices, employees etc.
public class Company{
private String company_name;
private int company_registration_number;
private String company_address;
public Company(String company_name, int crn, String company_address) {
this.company_name = company_name;
this.company_registration_number = crn;
this.company_address = company_address;
}
/* Getters, Setters, toString */
}
Department class
public class Department {
private String dept_name;
private String dept_description;
public Department( String dept_name, String dept_description) {
this.dept_name = dept_name;
this.dept_description=dept_description;
}
/*Getters, Setters, toString*/
}
CompanyDB
public class CompanyDB {
public boolean createCompany(Company company) throws SQLException {
String sql_query = "INSERT INTO " + DB_NAME + ".company VALUES (?, ?, ? )";
}
//other crud methods
}
DepartmentDB
public class DepartmentDB {
public boolean createDepartment(Department department, String company_name) throws SQLException {
String sql_query = "INSERT INTO " + DB_NAME +
".department(dept_name, dept_description, company_name)" +
" VALUES(?, ?, ?) " +
" WHERE company_name=?";
}
//other crud methods
}
SQL:
create table `company`(
`company_name` varchar(250),
`company_registration_number` int not null,
`company_address` varchar(250) not null,
primary key(`company_name`)
);
create table `department` (
`dept_id` int auto_increment,
`dept_name` varchar(250) not null unique,
`dept_description` varchar(512) not null,
`company_name` varchar(250),
primary key(`dept_id`) ,
foreign key(`company_name`) references `company`(`company_name`)
);
Upvotes: 1
Views: 58
Reputation: 110
You may want to check out Hibernate relations or annotations as this could make your life a lot easier.
See https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/associations.html or https://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/ for some basics.
It seems like you've got the basics down but Hibernate/JPA provides the functionality such that you only have to create the database tables and relations or the entities with mapping and it will automatically generate the other half for you.
An example of what you are trying to do
public class Company {
@Id
private Long id;
@Column(name = "company_name")
private String companyName;
@Column(name = "company_registration_number")
private int companyRegistrationNumber;
@Column(name = "company_address")
private String companyAddress;
@OneToMany
private Set<Department> departments;
/* Getters, Setters, toString */
}
and
public class Department {
@Id
private Long id;
@Column(name = "dept_name")
private String deptName;
@Column(name = "dept_description")
private String deptDescription;
@ManyToOne
private Company company;
/*Getters, Setters, toString*/
}
In this case Hibernate will automatically associate the Company with all of the Departments by creating a column on the Department table (on the primary key for the company) so that it can associate all departments with said Company.
Upvotes: 2
Reputation: 36
For JPA entities mapping is accomplished by annotations, also tables may be generated by Hibernate plugin please read about this. This link may help http://docs.jboss.org/tools/latest/en/hibernatetools/html/plugins.html
Upvotes: 0