From: tomo on 4 Jun 2010 09:52 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
From: Tom Anderson on 4 Jun 2010 14:29 On Fri, 4 Jun 2010, tomo wrote: > 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 -- There is no harm in being sometimes wrong especially if one is promptly found out. -- John Maynard Keynes
From: tomo on 4 Jun 2010 14:50 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
From: Lew on 4 Jun 2010 18:34 tomo wrote: > 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? <http://sscce.org/> > 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. -- Lew
|
Pages: 1 Prev: including apache http components in my program Next: Update GUI when selecting language |