Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to insert relational data in database using Hibernate?

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there
I am designing a Web Application and I am using Hibernate to save data in the database
this is my form

What I am trying is to save that data receive from JSP page to database.
I have configured database properties in Spring.xml file.

Now only think I want to know how can I insert this type of data in database.

The Points are
1) in My case field(Id,First_Name,Last_Name,DOB,Age,Email) these are unique

2)In High School and Inter School there is a block of data within it like(Institute Name,Board,Year of Completion)but this is fix.


3)There is a block for Past_Employment that contain field(Name of Organisation,Role,From,to) and this can be 1,more than 1 and sometime can be 0.

4)I am also saving Profile Picture in the database.

So I want to know what should be my table structure to save this data.

If My Statement is not clear please ask.I will be happy to answar it.

Thanks


 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:So I want to know what should be my table structure to save this data.

So you are actually asking how your database design (table structure) should look like to store the data you described. Or are you planning to create the database scheme based on your entity model?
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EDIT:I have added my form also with my OP so you can get an Idea what I am trying to do

I am somehow planning both table structure and also entity models.

from where I should start?
Reg_Form.PNG
[Thumbnail for Reg_Form.PNG]
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what's actually your problem? What are you struggling with?

Because now it seems you are just looking for someone who creates your database design (and entity model) for you. That's not how these forums work: CodeRanch is NotACodeMill and you should ShowSomeEffort.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok here is what I have done so far

this is my spring.xml file


and this is my Person model class that I am trying to persist in the database

(This is not fully design this class I am trying to save in database.

this is my DAO interface


and this is my Implementation class of PersonDao



What I am trying is
1)Get data from JSP page.
3)Design a Model class whose structure should be like my Incoming data.
3)Pack all Values in Person Object.
4)Save that data using PersonDaoImpl's save() method.

This is what I Have in mind.I hope you can understand and know help me in right direction.

Thanks
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use Spring. Did you consider using Spring data (instead of Dao pattern)?

Your data members are against java naming policy: java uses camelCase notation, no underscores.

Your Person entity doesn't match at all with the screenshot of your jsp. No marital status, no e-mail. A single address field (of type String) while according to your jsp, you could have several addresses with several fields. No father_name on your jsp.

If a user enter his date of birth, why does he has to enter his age as well?
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:You use Spring. Did you consider using Spring data (instead of Dao pattern)?


Can you explain what it mean.You mean Spring Data in place of Hibernate.

Your data members are against java naming policy: java uses camelCase notation, no underscores.

Sure I will update soon my code

Your Person entity doesn't match at all with the screenshot of your jsp. No marital status, no e-mail. A single address field (of type String) while according to your jsp, you could have several addresses with several fields. No father_name on your jsp.

As I have written below that this is Not my complete design be because I want to know
1)How I Should Store More than one Employment details?
2)How I should Store Two type of address(Permanent and current) as seprate while they relate with Adresss Object?
3)What I need to store if there is more than one mobile numbers and sometime only one
4)How to store a Picture in the database?

If a user enter his date of birth, why does he has to enter his age as well?

age will be dynamically calculate and added using JQuery by subtracting current_date-enter_age.

I hope you got my points
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:
Roel De Nijs wrote:You use Spring. Did you consider using Spring data (instead of Dao pattern)?


Can you explain what it mean.You mean Spring Data in place of Hibernate.

Have a look at Spring Data JPA. You still need Hibernate as the persistence provider.

Kishor Joshi wrote:As I have written below that this is Not my complete design be because I want to know
1)How I Should Store More than one Employment details?
2)How I should Store Two type of address(Permanent and current) as seprate while they relate with Adresss Object?
3)What I need to store if there is more than one mobile numbers and sometime only one
4)How to store a Picture in the database?

Have a look at the Java Persistence Cookbook. You'll need a OneToMany relationship voor employment details and mobile numbers.

In your Person entity, you'll have 2 data memeber of the Address entity with a ManyToOne relationshipWith this approach al your address data will be in a seperate address table. You could also use Embeddable and then address data will be in seperate columns in the Employee_Data table.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Roel De nijs

Ok
based on suggestion for persitance I have modified my code according to my requirement.Now I am just testing this code for Hibernate work not from Web Application this is my Spring.xml file


this is my main model class
Person


this is my Address Embeddable class


and same code I have written for Past_Emploment,High_School,Inter_School,Mobile Embedded Objects

This is my Class Path Resource class from where I am testing this class



My personDao class is Inserting all data in database the Only thing I am not getting is Insert Image in databases.

I have store Image in image/imageName like this(Check Attachment)


Eberything is storing in database except Images
How to store Images in the database?
directory structure.PNG
[Thumbnail for directory structure.PNG]
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:My personDao class is Inserting all data in database

Well done! Still a whole bunch of underscores in your code

Kishor Joshi wrote:Eberything is storing in database except Images
How to store Images in the database?

I don't see any code to convert the file on your file system to a byte array. I only see code which creates an (empty) byte array with the length of your file...
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Roel De Nijs


Thanks


I don't see any code to convert the file on your file system to a byte array. I only see code which creates an (empty) byte array with the length of your file...



How to do that?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:

I don't see any code to convert the file on your file system to a byte array. I only see code which creates an (empty) byte array with the length of your file...


How to do that?

This link will probably be helpful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic