-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: insert data using hibernate
PostPosted: Tue Aug 26, 2008 4:00 am 
Newbie

Joined: Thu Aug 14, 2008 2:22 am
Posts: 11
Hello together,

I store data for my database in files which are separated with \t. I want to insert this into my database (mysql) using hibernate. With the entitymanager, i can do something like that:

Address address = new Address();
address.setCountry("Country");
...
entityManager.persist(address);

But if i want to insert 100 lines of data, i have to do 100 times address.setSomething().
Is there an other possibility to insert data with hibernate.

Alex


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 12:18 am 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
Hi Alex

Sorry I didn't understand well your post but basically if you have a file with addresses and you want to insert them using the EntityManager, then yes for each address you will have to:

address = new Address ();
address.setX
address.setY

entityManager.persist (address);

I don't know any other way


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 4:52 am 
Newbie

Joined: Thu Aug 14, 2008 2:22 am
Posts: 11
Hi,
thank you very much for your reply. Here is my scenario for better understanding:
I have addresses and companies:

@Entity
@Table(name = "Address")
public class Address
{
@Id @GeneratedValue
@Column(name = "Address_ID")
private Long aid;

@Column(name = "Street")
private String street;

@Column(name = "Street")
private String street;

@OneToMany(mappedBy="address")
private Set<company> company = new HashSet<company>();
}

@Entity
@Table(name = "Company")
public class Company
{
@Id @GeneratedValue
@Column(name = "Company_ID")
private Long uid;

@Column(name = "Name")
private String name;

@ManyToOne
private Address address;
}

Now I want to create 100 companies and 100 addresses. To do add one company and one address I have to do the following:

Company comp = new Company();
comp.setName("Company1");
...
Address address = new Address();
address.setStreet("Street1");
...
entityManager.persist(address);
comp.addAddress(address);
entityManager.persist(comp);

Do I have to do this 100 times for 100 addresses and companies or is there an other (shorter, better) hibernate solution to do that?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 8:57 am 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
Hi

Ok that dependes on your cascade options in the relationship between Address and Company. If you configure Cascade.PERSIST then you only have to persist your Address Object. When you persist the Address object it will persist the Company object. Enable the show_sql option so you can see this effect. But you still have to persist 100 Addresses. This will depend on your business logic. Some application require that you save the objects separately, some applications require to save the two. So it all depends on your Cascade options.

Néstor


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2008 4:57 am 
Newbie

Joined: Thu Aug 14, 2008 2:22 am
Posts: 11
Hi,
thanks for your reply. Cascade.PERSIST sounds good.
But what about the foreign keys. this could be very complicated to add the foreign keys?
Alex


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.