Nested custom object in Beans | Spring Boot and MicroServices Forum
S
Sailee Naik Posted on 27/09/2020

I have added a custom object Communicaiton under User bean ..my goal is to add two new fields email and phone ....i have added them as shown below..

I want trying to test phone number formatter but i am getting exception for the COmmunicaiton object 

i am getting below error message...can you please help fix it 

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: com.sailee.beans.Communication, at table: user, for columns: [org.hibernate.mapping.Column(communication)]

User.java

{private String firstName;
private String lastName;
private Date dateOfBirth;
private Communication communication;}

Communicaiton.java

{private email; Phone phone}

Phone.java

{private String countryCode; private String phoneNumber}

 


Y
Yogesh Chawla Replied on 27/09/2020

Sailee,

As of now, 'User' table in database has got only three columns let's say firstName, lastName and dateOfBirth and let's say you have also created User Bean class in your Spring code using @Entity annotation like the way we have shown in our code.

Now You want to introduce 'Communication' Bean inside 'User' bean so first User bean will have the reference to Communication and then Communication bean will be separetly created with these two fields email and phone using @Entity annotation and You have created a separate table in database with the same name 'Communication' with these two same fields email and phone.

And You want to create one more bean class with the name 'Phone' with fields countryCode and phoneNumber so again You have create a separate bean class and another table in database with the name 'Phone' with these two very fields(countryCode and phoneNumber)


Now basically, we want to link these three tables:

User with Communication and further Communication with Phone.

As I said, Please create three @Entity classes and three tables in database.

Something like this

@Entity
@Table(name = "User")
public class User {

@Id
@Column(name = "user_id", columnDefinition = "user_id")
private Long userId;

@ManyToMany
@JoinTable(
name = "User_Communication",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "user_id"),
inverseJoinColumns = @JoinColumn(name = "communication_id", referencedColumnName = "communication_id"))
@JsonIgnore
private List<Communication> comm;

//Getters and setters
}


@Entity
@Table(name = "Communication")
public class Communication {

@Id
@Column(name = "communication_id", columnDefinition = "communication_id")
private Long communicationId;

@ManyToMany
@JoinTable(
name = "Communication_Phone",
joinColumns = @JoinColumn(name = "communication_id", referencedColumnName = "communication_id"),
inverseJoinColumns = @JoinColumn(name = "phone_id", referencedColumnName = "phone_id"))
@JsonIgnore
private List<Phone> phone;


//Getters and setters
}

Phone.java

@Entity
@Table(name = "Phone")
public class Phone {

@Id
@Column(name = "phone_id", columnDefinition = "phone_id")
private Long phoneId;

@Column(name = "countryCode", columnDefinition = "countryCode")
private String countryCode;

@ManyToMany
@JoinTable(
name = "Communication_Phone",
joinColumns = @JoinColumn(name = "phone_id", referencedColumnName = "phone_id"),
inverseJoinColumns = @JoinColumn(name = "communication_id", referencedColumnName = "communication_id"))
@JsonIgnore
private List<Communication> comm;

//Getters and setters
}

Something Like this, three bean classes will be linked:

And then we will have to write query using @Query annotation in @Repository class combining these three tables to fetch our data. This we have already seen in our code.

Query can be written something like this:

select <columns>
from User user0_
left outer join communication uc1_ on user0_.user_id=uc1_.user_id, user0_.communication_id=ph1_.communication_id
left outer join Phone up1_ on user0_.user_id=up1_.user_id, user0_.phone_id=up1_.phone_id


You can use @OneToOne/@OneToMany relationship annotation also from User to Communication to Phone depending on your data but make sure ids and fields match like how we are representing in code


S
Sailee Naik Replied on 28/09/2020

Thanks Yogesh for detailed explanation.

I will try to implement the way you have described.

If you uploading new tutorials in future , please do touch base on this concept in video recordings.

 


Y
Yogesh Chawla Replied on 29/09/2020

Sure Sailee. I will add this very mutil level table concept also in videos.