Janik von Rotz


4 min read

Build a Java 3-tier application from scratch - Part 2: Model setup

Last time we’ve set up our basic project structure with gradle. This time we are going to create the models aka our Java classes. Below is uml diagram showing all classes, interfaces and their relationship.

3-tier java application Classmodel

Common Eclipse

Open the common project in eclipse and update it according to the instructions.

1) Create a project structure like this:

Common Eclipse Filestructure

2) Update the class and interface files with the content showed below.

DAO.java

package ch.issueman.common;

import java.io.Serializable;
import java.util.List;

public interface DAO<T, Id extends Serializable> {
	
	public void persist(T t);
	public T getById(Id id);
	public List<T> getAll();
	public void update(T t);
	public void delete(T t);
	public void deleteAll();
}

We will use this interface later on for our controllers. It simply defines the allowed methods to communicate between the layers.

Model

package ch.issueman.common;

public interface Model {

	public int getId();
}

No models without the models interface. For a DRY(don’t repeat yourself) code approach we will use polymorphism and a generic controller.

Person.java

package ch.issueman.common;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

import lombok.Data;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

@Entity
@Data
@Inheritance(strategy=InheritanceType.JOINED)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Person implements Model{
	
	@Id
	@GeneratedValue
    private int id; 
    private String name;
	
	public Person(){}
	
	public Person(String name) {
		super();
		this.name = name;
	}
}

This is the parent class for person related objects.

User.java

package ch.issueman.common;

import javax.persistence.Entity;

import lombok.Data;

@Entity
@Data
@EqualsAndHashCode(callSuper=false)
public class User extends Person {

	private String email;
	private String password;
	private String role;
	
	public User(){}
	
	public User(String name, String email, String password, String role) {
		super(name);
		this.email = email;
		this.password = password;
		this.role = role;
	}
}

This is a child class of the Person class. We will use this class to authenticate the client against the webservice later.

Employer.java

package ch.issueman.common;

import javax.persistence.Entity;

import lombok.Data;

@Entity
@Data
@EqualsAndHashCode(callSuper=false)
public class Employer extends Person {

	private String company;
		
	public Employer(){}
		
	public Employer(String name, String company) {
		super(name);
		this.company = company;
	}
}

Another child class of the Person class. Every project is owned by an employer.

Comment.java

package ch.issueman.common;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;

import lombok.Data;

@Entity
@Data
public class Comment implements Model{
	
	@Id
	@GeneratedValue
	private int id;
	@Lob
	private String comment;
	@ManyToOne
	private User user;
	
	public Comment(){}
	
	public Comment(String comment, User user) {
		super();
		this.comment = comment;
		this.user = user;
	}
}

A project can have multiple commands written by a user.

Project.java

package ch.issueman.common;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

import lombok.Data;

@Entity
@Data
public class Project implements Model {

	@Id
	@GeneratedValue
	private int id;
	private String title;
	@ManyToOne
	private Employer employer;
	@ManyToMany(cascade = CascadeType.ALL)
	private List<Comment> comments;

	public Project() {
	}

	public Project(String title, Employer employer) {
		super();
		this.title = title;
		this.employer = employer;
	}
}

A project contains multiple comments and is owned by an employer.

Here’s a list of all cascade types:

3) That was it. If you got this far you’ve successfully created the required models for our project.

Update

Links

Source

[Feedback from Reddit on this post]()

Categories: Software development
Tags: three tier , application , eclipse , java
Edit this page
Show statistic for this page