Use a generic data object (and table) for several #OneToOne mappings - java

Instead of having several small objects (which have mostly the same properties like ID and NAME) mapped as #OneToOne, I would like to have one "generic" kind of object which is mapped to a single database table, and should be referenced by a kind of discriminator column group_id in this case.
The code examples should make it clear what I want to achieve:
#Entity
#Table(name = "GENERAL_DATA")
public class GeneralData {
#Id
#Column(name = "GROUP_ID")
private int groupId;
#Id
#Column(name = "ID")
private int id;
#Column(name = "NAME")
private String name;
}
#Entity
#Table(name = "INVOICE")
public class Invoice {
#OneToOne
#JoinColumn(name = "STATUS")
private GeneralData status;
#OneToOne
#JoinColumn(name = "COLOR")
private GeneralData color;
}
The database table structure of GeneralData should be like this:
group_id id name
-----------------------------
1 1 Active
1 2 Processing
1 3 Cancelled
2 1 Blue
2 2 Green
2 3 Red

Why won't you get rid of GeneralData entity and use simple enums in invoice entity? If you have specified Statuses / Colors it is better way.
#Entity
#Table(name = "INVOICE")
public class Invoice {
#Column(name = "STATUS")
private Status status;
#Column(name = "COLOR")
private Color color;
}
public enum Status {
ACTIVE,
PROCESSING,
CANCELLED
}
// ...

Related

Spring data JPA save and updating parent entity

I have two entities called Student and Subject. They are stored in tables in the following format
student_id
name
grade
1
John
1
subject_id
name
1
English
2
Math
subject_id
student_id
mark
1
1
75
2
1
75
**Student:**
#Table(name = "student")
#Data
public class Student {
#Id
#GeneratedValue(strategy = IDENTITY)
private Long id;
#Column(name = "name")
private String name;
#Column(name = "grade")
private int grade;
//getters and setters left out for this example
}
**Subject:**
#Table(name = "subject")
#Data
public class Subject {
#Id
#GeneratedValue(strategy = IDENTITY)
private Long id;
#Column(name = "name")
private String name;
//getters and setters left out for this example
}
**StudentRepository:**
public interface StudentRepository extends JpaRepository<Student, Long> {
}
How do I make it so that everytime I add a student using a StudentController, the subjects are automatically added to the student.
Create the third entity for the third table, create the student object and the subject object . put it in the third entity object, create the third repository and save that, all three tables will be updated together. Just make sure your relationships are correctly mentioned and you are done.
Update your Student entity to encapsulate Subject.
The idea is to explicitly define relationship between Student and Subject , and further leverag Cascade to propagate changes :
#Table(name = "student")
#Data
public class Student {
#Id
#GeneratedValue(strategy = IDENTITY)
private Long id;
#Column(name = "name")
private String name;
#Column(name = "grade")
private int grade;
#OneToOne(cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE}, orphanRemoval = true)
#JoinColumn(name = "SUBJECT_ID")
Subject subject;
}
Note : You need to make sure that you populate Subject when storing Student.
For more clarity , explore the examples presented here : https://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/

How to model compound key in Hibernate?

I am modeling a model class in java with Hibernate. But the table in the DB already exists and I want to use it.
The problem is that the table do not have an ID. Besides, the worse is that the tuple in the table are formed by four columns and their combination makes a "kind of identification/ID". Also, the columns are not FK to others tables.
Is there a way to model this with java, hibernate, and so on, without "fix/alter" the table?
Thanks
You have to define your class with an #EmbeddedId Example:
Your Entity that already exists :
#Entity
#Table(name = "ENTITY_EXAMPLE")
public class EntityExample {
// this is your new class Embeddable
#EmbeddedId
private EntityExampleId id;
//other fields
//geters y seters
//////////////////////////
}
And have to create other Class #Embeddable that contains your 4 columns of your composite ID
#Embeddable
public class EntityExampleId implements Serializable{
private static final long serialVersionUID = 1L;
#Column(name = "COLUMN1", nullable = false)
private Integer col1;
#Column(name = "COLUMN2", nullable = false)
private Integer col2;
#Column(name = "COLUMN3", nullable = false)
private Date col3;
#Column(name = "COLUMN4", nullable = false)
private Date col4;
//geters y seters
//////////////////////////
}
For more info see the references
reference 1
reference 2
reference 3

How to define the association table as pojo entity

I have 3 tables in the db. I am trying to write the JPA entities. I am facing some issues with Association table entity. My entities are as follows,
Person.java
#Entity
#Table(name = "person")
public class Person {
#Id
#GeneratedValue
private Long id;
#Column(nullable = false)
private String firstName;
#Column(nullable = false)
private String lastName;
//setter and getter
}
Exam.java
#Entity
#Table(name = "exam")
public class Exam {
#Id
#GeneratedValue
private long examId;
#Column(nullable = false)
private String examName;
#Column(nullable = false)
private int marks;
//Setters and getters
}
The table structure for association table is,
create table person_exam (
personId BIGINT not null,
examId BIGINT not null,
primary key (personId, examId)
);
I tried the association table entity with #ManyToMany annotation for both the properties which is not giving me the result.
Can anyone please suggest me what should I need to use (ManyToMany/OneToOne/ManyToOne/OneToMany ) in my entity for the above person_exam table.
from the PRO JPA 2nd Ed. book:
the only way to implement a many-to-many relationship is with a separate join table. The consequence of not having any join columns in either of the entity tables is that there is no way to determine which side is the owner of the relationship. Because every bidirectional relationship has to have both an owning side and an inverse side, we must pick one of the two entities to be the owner.
So I chose the the Person entity. Applying the needed changes to your incomplete code:
#Entity
#Table(name = "person")
public class Person {
#Id
#GeneratedValue
private Long id;
#Column(nullable = false)
private String firstName;
#Column(nullable = false)
private String lastName;
/**
* we need to add some additional metadata to the Person designated
* as the owner of the relationship, also you must fully specify the names of
* the join table and its columns because you already provided a schema
* for the association table, otherwise the JPA provider would generate one.
*/
#ManyToMany
#JoinTable(name="person_exam",
joinColumns=#JoinColumn(name="personId"),
inverseJoinColumns=#JoinColumn(name="examId"))
private Collection<Exams> exams;
//setter and getter
}
#Entity
#Table(name = "exam")
public class Exam {
#Id
#GeneratedValue
private long examId;
#Column(nullable = false)
private String examName;
#Column(nullable = false)
private int marks;
//Setters and getters
/**
* As in every other bidirectional relationship,
* the inverse side must use the mappedBy element to identify
* the owning attribute.
*/
#ManyToMany(mappedBy="exams")
private Collection<Person> people;
}

Many to one Hibernate mapping

So, here'e the situation:
Table ComputerInventory with {computerInventoryID (Primary key), TagID(unique), Name etc}
Table reviewStatus with {reviewStatusID(Primary key), computerInventoryID (ForeignKey), status }
I've written the hibernate Entity for ReviewStatus:
public class ReviewStatus implements Serializable {
public enum reviewStatus {
To_Be_Reviewed,
Reviewed
}
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long reviewStatusId;
#Column(name = "lastModifiedTime")
private Date lastModifiedTime;
#Column(name = "Comments")
private String comments;
#Enumerated(EnumType.STRING)
#Column(name = "status")
//all above params have gettetrs and setters
private reviewStatus status;
//TODO: Mapping to computerinventory
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "computerInventoryId")
//For one ComputerInventoryID, there can be many review Statuses.
private Set<ReviewStatus> reviewStatusSet;
public long getreviewStatusId() {
return reviewStatusId;
}
My Doubts:
For one ComputerInventoryID, there can be many review Statuses, so do I have a
Set<ReviewStatus> reviewStatusSet
where I return the list of entries in reviewstatus? Sorry, but I don't understand how I can write a get/set for returning and setting the reviews status of a bunch of records.
Your reference from ReviewStatus should be to a ComputerInventory, not to its ID. Hibernate lets you abstract out the details of the primary key (the ID), letting you directly reference from one object to another. You should use an #ManyToOne annotation on your private ComputerInventory computerInventory;.

Spring Hibernate product - category relationship

I've read many tutorials about spring-hibernate relationships but I'm a bit confused about how to use them in my case... I've product/category entities defined as follow:
Product
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column
private int id;
#Column
private int category;
.
.
.
Category
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column
private int id;
#NotEmpty
#Column
#Size (max = 25)
private String name;
.
.
.
So, I'd like in the product list page, under the voice "category" would appear the category name, and in the product form the category list...
In my case a product fits only one category so if I'm right it should be a #ManyToOne but I don't know how to implement this... in my product database I've the categoryId field, but if I mark the category entity field as #OneToMany it will not be stored to the db...
EDIT
I've changed like this (as suggested):
Product.class
#Table(name = "products")
public class Product {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column
private int id;
#NotEmpty
#Column
#Size (max = 25)
private String name;
#Column
#Size (max = 255)
private String description;
#ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
#JoinColumn(name = "category_id", nullable = false)
private Category category;
Category.class
#Entity
#Table(name = "categories")
public class Category {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column
private int id;
#NotEmpty
#Column
#Size (max = 25)
private String name;
#Column
#Size (max = 255)
private String description;
//Here mappedBy indicates that the owner is in the other side
#OneToMany(fetch = FetchType.EAGER, mappedBy = "category", cascade = CascadeType.ALL)
private Set<Product> products = new HashSet<Product>();
Controller
#RequestMapping(value = "/add/", method = RequestMethod.POST)
public String addProduct(
#ModelAttribute(value = "product") #Valid Product product,
BindingResult result, ModelMap model, Category category) {
if (result.hasErrors()) {
return "forms/productForm";
}
try {
category.addProduct(product);
product.setCategory(category);
// Add product to db
productService.addProduct(product);
} catch (Exception e) {
log.error("/add/---" + e);
return "redirect:/product/deniedAction/?code=0";
}
return "redirect:/admin/product/";
}
I also added a #initbinder on the product controller to translate the data from the product form string to Category... but now when I save a product it automatically saves a category instead of attach the existing selected one...
As the Product will have only one Category and Category will have a list of Products, you can relate these two by creating a Foreign Key in the Product table to refer to the primary key in the Category table:
Category Table: id, name, other fields...
Product Table: id, category_id (FK), and other fields.
And the mapping can be defined as below:
public class Category {
//Here mappedBy indicates that the owner is in the other side
#OneToMany(fetch = FetchType.LAZY, mappedBy = "category", cascade = CascadeType.ALL)
private Set<Product> products = new HashSet<Product>();
...
}
public class Product {
//Here JoinColumn states that this entity is the owner of the relationship
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "category_id", nullable = false)
private Category category;
...
}
The mappedBy attribute tells Hibernate that the collection is a mirror image of the many-to-one association on the other side. Its like telling Hibernate that it should propagate changes made at the Product end of the association to the database, ignoring changes made only to the products collection that you have in the Category. Thus if we only call category.getProducts().add(product), no changes will be made persistent. As the association is bidirectional, you have to create the link on two sides, not just one.
For your convenience, you can add one addProduct method in the Category class to save the association:
public void addProduct(Product product) {
product.setCategory(this);
products.add(product);
}
You appear to have a one-to-many relationship between Category and Product (one category has many products)
In Java (and OO generally) you'd expect the Category class to contain a list of Products, so the Category can be said to 'own' products.
In SQL it's the other way round - you'd expect Product table to hold a foreign key reference to a Category, so here, the Product can be said to 'own' a Category.
Looks like your using JPA, so you could have something like this:
Category class:
#Entity
public class Category {
//other stuff...
#OneToMany(cascade=CascadeType.ALL, mappedBy="category")
private Set<Product> products;
}
Product class:
#Entity
public class Product {
//other stuff...
#ManyToOne
private Category category;
}
so you have this:
Product{
atributtes...
#ManyToOne
Category category; --so every product has a category
}
Category {
attributtes...
#OneToMany(cascade=CascadeType.ALL)
#JoinColumn(name="id_Product")
private List<Product> products;
}
try this, if not we can look another solution..
You are right, you should use #ManyToOne because "...a product fits only one category...".
In Product entity declare a Category field instead of int category and annotate it with #ManyToOne. Also add #JoinColumn to specify the name of product.category_id column in the database.
Product:
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column
private int id;
#ManyToOne
#JoinColumn(name = "category_id")
private Category category;
.
.
.

Categories