Create Relation with tables using JPA & Hibernate (Spring) - hibernate

I need create this relation (Figure 1) with this tables:
Figura 1
The relation is Professor 1-N Disciplina.
My tables code is:
Disciplina:
#Entity
#Table(name = "TBL_DISCIPLINA")
public class Disciplina {
#Id
#Column( name = "ID_DISCIPLINA")
#NotNull
long codigo;
#NotNull
#Column( name = "ST_NOME_DISCIPLINA")
#Size(max = 100)
String nome;
#NotNull
#Column( name = "ST_NOME_PROFESSOR")
#Size(max = 100)
String prof;
#NotNull
#Column( name = "URL_IMG")
String img;
//I will add gets and sets here
}
Professor:
#Entity
#Table(name = "TBL_PROFESSOR")
public class Professor {
#Id
#Column(name = "ID_PROFESSOR")
#NotNull
long id;
#NotNull
#Column( name = "ST_NOME_PROFESSOR")
#Size(max=100)
String nome;
#NotNull
#Column( name = "ST_ENDERECO")
#Size(max=300)
String endereco;
#NotNull
#Column( name = "CO_EMAIL")
#Size(max=100)
String email;
#NotNull
#Column( name = "CO_TELEFONE")
#Size(max=11)
String telefone;
#NotNull
#Column( name = "DT_DATA_NASC")
#Size(max=100)
Date dataN;
#NotNull
#Column( name = "DC_CPF")
#Size(max=11)
String cpf;
#NotNull
#Column( name = "DC_RG")
#Size(max=11)
String rg;
#Column( name = "URL_IMG_ALUNO")
String url;
#NotNull
#Column( name = "PW_SENHA_PROFESSOR")
#Size(max=50)
String senha;
//I will add gets and sets here
}
Idk how to make #ManyToOne and OneToMany with this 2 tables. I need a example to do this relation.
how can I interconnect these two tables? Should I do it with #ManyToOne or #OneToMany? Is it necessary to add extra fields for foreign key? if so, how should I do this?

This article shows step by step, How to create mapping one to many and many to one. Also explains the bidirectional relationship for hibernate JPA
https://www.callicoder.com/hibernate-spring-boot-jpa-one-to-many-mapping-example/

Related

Join #ManyToOne hibernate using composite Key

I have this table
and I have another table which uses only CODE upon type of M_CODE
so this is mapped to
this two classes
#Entity
#Table(name = "PIS_DOMAIN_DETAIL")
public class Domain implements Serializable{
#Id
DomainId id;
#Column(name = "CODE" , insertable = false, updatable = false)
private Integer code;
#Column(name = "COM_NO")
private Integer comNo;
#Column(name = "DESC_A")
private String descA;
#Column(name = "DESC_L")
private String descL;
#Column(name = "TIMESTAMP")
private Date timestamp;
#Column(name = "USER_ID")
private Integer userId;
and this is embeddable
#Embeddable
public class DomainId implements Serializable{
#Column(name = "M_CODE")
private Integer mCode;
#Column(name = "CODE")
private Integer code;}
so in Other table that use only code in column WF_STATUS i have created this
// this is select id.code from Domain where id.mCode=35
#Column(name = "WF_STATUS") //that recievce CODE from domain entity
private Integer wfStatus;
#ManyToOne
#JoinColumn(name = "WF_STATUS", referencedColumnName = "CODE",
insertable = false, updatable = false )
#Where(clause=" id.mCode = 35 ")
Domain status;
but this out this error
More than one row with the given identifier was found: com.project.entities.Domain#50bc3219, for class: com.project.entities.Domain
how i can edit my map to accept this case .. thanks in advance ...
You have a duplicate column, you should decide whether code column will be in the DomainId as a part of the compound key or not (only in the Domain class).

Hibernate. OneToOne mapping problems

#Entity
#Table(name="Customer")
public class Customer implements Serializable{
/**
* This is my Customer entity object
*/
private static final long serialVersionUID = 9037154862690840147L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name="Cust_Id")
private int customerId;
#Column(name="First_Name")
private String firstName;
#Column(name="Last_Name")
private String lastName;
#Column(name="Gender")
private String gender;
#Column(name="Email")
private String email;
#Column(name="Password")
private String password;
#Column(name="City")
private String city;
#Column(name="State")
private String state;
#Column(name="Country")
private String country;
#Column(name="Phone_No")
private String phoneNumber;
#Column (name="Zip_Code")
private int zipCode;
#Column(name="LAST_UPDATED_NM")
private String lastUpdatedName;
#Column(name="LAST_UPDATED_DTM")
private Date lastUpdatedTime;
#OneToOne(mappedBy = "customer", cascade = CascadeType.ALL)
private RoleOfUser roleOfUser;
This is my Role of user entity object
#Entity
#Table(name = "role_of_user")
public class RoleOfUser implements Serializable{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "Role_Id")
private Integer roleId;
#Column(name = "Role_Name")
private String roleName;
#Column(name = "First_Name")
private String firstName;
#Column(name="Last_Name")
private String lastName;
#Column(name = "Created_NM")
private String createdName;
#Column(name = "Created_DTM")
private Date createdDate;
#Column(name = "Last_Update_NM")
private String lastUpdatedName;
#Column(name = "Last_Update_DTM")
private Date lastUpdatedDate;
#Column (name = "Status")
private String status;
#OneToOne(cascade = CascadeType.ALL)
#PrimaryKeyJoinColumn
private Customer customer;
#Column(name = "Customer_Cust_Id")
#GeneratedValue(generator="gen")
#GenericGenerator(name="gen", strategy="foreign", parameters = #Parameter(name = "property", value = "customer"))
private Integer customerId;
customerId from customer entity table need to be a foreign key to Customer_cust_id in RoleOfUser table.
i ran the above code and it says Customer_cust_id cannot be null.
How do i map foreign keys?
Role_id is already a primary key.
Using primitives in my persistence classes has always been a pet peeve of mine because it can lead to some misleading errors, so I'd start with changing int customerId to Integer customerId.
Next, it sounds like Customer should be your parent table and RoleOfUser should be the child. The problem here is that you are saying your tables are one-to-one, but you don't then join on the #Id column of RoleOfUser.
Consider doing something like this instead:
#Table(name = "CUSTOMER")
public class Customer {
#Id
#Column(name = "CUST_ID")
private Integer customerId;
}
#Entity
#Table(name = "ROLE_OF_USER")
public class RoleOfUser {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ROLE_ID")
private Integer roleId;
#OneToOne
#JoinColumn(name = "CUSTOMER_CUST_ID", referencedColumnName = "CUST_ID")
private Customer customer
}
Finally, though, if these tables are truly one-to-one, why not just make them one wider table with more columns? Or if a user is allowed to have multiple roles, then you'll want to change that #OneToOne to be an #ManyToOne.

#IndexedEmbedded on logical relationship

I have two tables (external, so I can not modify it). Tables has logical relationship (one has reference ID for other) only, without #OneTo* annotation.
Is it possible for Hibernate Search index this related objects?
The code sample:
#Entity
#Indexed
#Table(name = "E1")
public class E1 implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
private Long id;
#Column(name = "NAME", length = 32, nullable = false)
private String name;
#Column(name = "E2_FK", nullable = false)
#IndexedEmbedded // ????
private Long e2Id;
}
#Entity
#Table(name = "E2")
public class E2 implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
private Long id;
#Column(name = "DESCRIPTION", length = 4000, nullable = false)
#Field
private String description;
}
No, Hibernate Search can in this case not index the associated object. It does not know anything about it. Your mapping does not contain any pointer for Hibernate (Search) that e2Id is the primary key to another entity. #IndexedEmbedded is for associated entities.
Why don't you make this true associations. You can have them lazy loaded per default in case that is one of your concerns.

Hibernate Duplicate entry for pirmarky key of referenced entity on using composite primary key and #MapsId

I have the following entities and is using hibernate 4.1.* as the JPA provider but using an composite primary key in customer table which maps to the user gives me
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry for key 'PRIMARY'
The entities mappings are as below.
#Entity
class Customer {
#EmbeddedId CustomerId id;
boolean preferredCustomer;
#MapsId("userId")
#JoinColumns({
#JoinColumn(name = "first_name_", referencedColumnName = "first_name"),
#JoinColumn(name = "last_name", referencedColumnName = "last_name")
})
#ManyToOne(optional = false)
private User user;
-----
}
#Embeddable
private UserPK userId;
#Basic(optional = false)
#NotNull
#Column(name = "customer_number")
private int customerNumber;
----
}
#Entity
class User {
#EmbeddedId UserId id;
#Basic(optional = false)
#NotNull
#Column(name = "age")
private int age;
------
}
#Embeddable
class UserId implements Serializable {
#Basic(optional = false)
#NotNull
#Size(min = 1, max = 45)
#Column(name = "first_name")
private String firstName;
#Basic(optional = false)
#NotNull
#Size(min = 1, max = 45)
#Column(name = "last_name")
private String lastName;
-------
}
The same mapping works with eclipselink. Also it works if I remove UserId from CustomerId and just reference it, but I want to solve this using hibernate and #MapsId
I tried using different cascade option as per the suggestion in JPA 2 - Foreign key that only includes one field from a composite primary key? but there was no luck.

Spring-data JPA repository Order by losing null values in results

I am using spring-data and a jpa repository for my queries. I am having a problem where, I have an entity with a ManyToOne field, if I order by this field in a query, then any values that have a Null for this field are not returned in my list. This doesn't seem like proper behaviour.
Here are what my entities look like:
#Entity
public class Item {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#NotNull
#Column(name = "id")
private Integer id;
#Size(max = 255)
#Column(name = "name")
private String name;
#JoinColumn(name = "owner_user_id", referencedColumnName = "id")
#ManyToOne(fetch = FetchType.LAZY)
private User ownerUserId;
}
Then the ManyToOne user entity
#Entity
public class User {
#Size(max = 100)
#Column(name = "email")
private String email;
#Size(max = 256)
#Column(name = "first_name")
private String firstName;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#NotNull
#Column(name = "id")
private Integer id;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "ownerUserId", fetch = FetchType.LAZY)
private Collection<Item> itemCollection;
}
I have my JPA repository like so:
#Transactional
public interface ItemRepository extends JpaRepository<Item, Integer> {
#Query("FROM Item i where name = ?1");
Page<Item> findItemWithName(String name, Pageable pageable);
}
I have simplified a lot of the code just so you can get an idea. All the queries are working great, the problem arises when I set the Sort in my Pageable object to sort on the owner_user_id column. If any of the entries in the item table have a null for owner_user_id they are not returned in the list.
Is there some sort of annotation I can add to get around this? Or something else I can do? I really want to keep using the repository but don't think I will if I can't get around this. I am using hibernate and MYSQL, not sure if that is part of the issue.
Thanks.
This was a known issue in Spring Data / JPA Spec which is solved in 1.2.1 Version. See https://jira.springsource.org/browse/DATAJPA-252 and https://jira.spring.io/browse/DATAJPA-277.

Resources