hibernate find method ?

T

tomo

Let's say I have Person entity which has Company entity(FK). And I have a
function that takes company type as an argument.Is it better to use

public void persistEntity(Short type){

Company company = new Company();
company.setType((short)1);
Person person = new Person();
person.setCompany(company);
entityManager.persist(person);
}

or

public void persistEntity(Short type){

Company company = entityManager.find(1,Company.class);
Person person = new Person();
person.setCompany(company);
entityManager.persist(person);
}

I have noticed that they do the same, but the first one is much
faster.Thanks.




public class Person{

private Long id;
private Company company;
private String name;




}

public class Company{

private Short type;
private String name;

}



__________ Information from ESET NOD32 Antivirus, version of virus signature database 5171 (20100604) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
T

Tom Anderson

public void persistEntity(Short type){
Company company = new Company();
company.setType((short)1);
Person person = new Person();
person.setCompany(company);
entityManager.persist(person);
}

or

public void persistEntity(Short type){

Company company = entityManager.find(1,Company.class);
Person person = new Person();
person.setCompany(company);
entityManager.persist(person);
}

I have noticed that they do the same, but the first one is much
faster.

No you haven't. The code you have actually posted is nonsense - both
methods take a parameter, but never use it. This is clearly not the actual
code you're running, so you can't have noticed anything about it. How
about posting the actual code?

Even if we fixed that, using the type parameter instead of the literal 1,
and if we fixed the order of the arguments to find, which you have wrong,
we have two methods which don't do the same thing. The former creates a
new company, the latter adds a reference to an existing company.

So, how about telling us what you're really doing?

tom
 
T

tomo

I'm sorry, I wrote those classes just as an example.I have now changed the
param to show what I really mean. I know that these to methods doesn't do
the same thing, but they both save new Company record in DB, doesn't they ?
I'm just asking which method is more let's say some persist pattern oriented
, and which is not , and why is method 1 faster than method two in persiste
new entity if child entity is already in DB ?


public void persistEntity(Short type){
Company company = new Company();
company.setType(type);
Person person = new Person();
person.setCompany(company);
entityManager.persist(person);
}

or

public void persistEntity(Short type){

Company company = entityManager.find(type,Company.class);
Person person = new Person();
person.setCompany(company);
entityManager.persist(person);
}


public class Person{

private Long id;
private Company company;
private String name;




}

public class Company{

private Short type;
private String name;

}







__________ Information from ESET NOD32 Antivirus, version of virus signature database 5172 (20100604) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
L

Lew

tomo said:
I'm sorry, I wrote those classes just as an example.I have now changed the
param to show what I really mean. I know that these to methods doesn't do
the same thing, but they both save new Company record in DB, doesn't they ?

No, they don't. Clearly not. In fact, near as I can tell, neither one does.

Have you considered making a complete example?
I'm just asking which method is more let's say some persist pattern oriented
, and which is not , and why is method 1 faster than method two in persiste
new entity if child entity is already in DB ?

Method one is faster because it doesn't check for the existence of the
'Company' in the data store, risking an 'EntityNotFoundException' or equivalent.

Both code samples you show are junk.

And which entity do you think is the "child" entity, 'Person' or 'Company'?
Because both samples imply that the child entity ('person') is not already in
the DB.
public void persistEntity(Short type){
Company company = new Company();
company.setType(type);

This will only work if the 'Company' is already in the data store.
Person person = new Person();
person.setCompany(company);
entityManager.persist(person);
}

or

public void persistEntity(Short type){

Company company = entityManager.find(type,Company.class);
Person person = new Person();
person.setCompany(company);

This will only work if the 'Company' is already in the data store.
entityManager.persist(person);
}

Now suddenly you're giving class definitions? What class was the above code in?
public class Person{

private Long id;
private Company company;

Where are your annotations?
private String name;

}

public class Company{

private Short type;
private String name;

}

JPA will only manage the relationships correctly if you map them.

Having a 'Short' for a "type" is a misnomer and an antipattern.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top