reset password
Author Message
Vanquish39
Posts: 134
Posted 00:08 Oct 15, 2011 |

Hi Professor, I'm having some problems creating the tables for a practice database. 


I checked out hibernate folder from the repository as a maven project. 

I went to persistence.xml and changed the information to my needs.  I created a practice database in postgres so I can play around with hibernate.  What I'm confused on is where is the actual command to create these tables to the database.  I run employeeTest and I get random errors.  Where is the creation of these tables taking place....and where do I click/run the code so my database can get populated by these tables. 

 

or... are we supposed to create the mappings, do the maven build --> process-classes -->  Get that script and execute it inside postgres? 


 

Last edited by Vanquish39 at 00:12 Oct 15, 2011.
cysun
Posts: 2935
Posted 00:11 Oct 15, 2011 |
Vanquish39 wrote:

Hi Professor, I'm having some problems creating the tables for a practice database. 


I checked out hibernate folder from the repository as a maven project. 

I went to persistence.xml and changed the information to my needs.  I created a practice database in postgres so I can play around with hibernate.  What I'm confused on is where is the actual command to create these tables to the database.  I run employeeTest and I get random errors.  Where is the creation of these tables taking place....and where do I click/run the code so my database can get populated by these tables.  Thanks.

The db scripts are under src/main/scripts.

Vanquish39
Posts: 134
Posted 00:13 Oct 15, 2011 |

Can I use this instead...

In my previous hibernate experience, I was using new SchemaExport(config).create(true,true).  This was actually persisting the tables to the database.  Problem is, I needed a hibernate.xml file.


It was something like...

Configuration config = new Configuration();

config.addAnnotatedClass(Employee.class)

config.configure();

SessionFactory factory = config.buildSessionFactory();

new SchemaExport(config).create(true,true)


This would persist everything straight to the database without a script.

Last edited by Vanquish39 at 00:14 Oct 15, 2011.
Vanquish39
Posts: 134
Posted 00:18 Oct 15, 2011 |

So If I don't have the scripts in src/main/scripts, I would have to do a maven build --> process classes --> then get that script and execute it in the database manually? 

cysun
Posts: 2935
Posted 00:53 Oct 15, 2011 |

I never export the schema directly to the database because

1. without looking at the ddl it's hard to check whether the schema is correct or not.

2. the schema always needs some customization, e.g. set sequence range, add some additional constraints and so on.

3. you'll still need scripts to populate the tables, create indexes, stored procedures, views etc.

4. you can't run the code if the database already exists. e.g. it's usually very helpful to do bug fixes using a copy of the production database.

5. it's hard to keep track of the evolution of the schema without a script.

In fact, I can't think of any good reason to do it for a real world application. The only place for it seems to be in examples (which usually use an in-memory database) and maybe some unit tests.

And on top of that it's a Hibernate specific thing.

If you really want to do it, check out the 2nd to last solution here. It's basically uses SchemaExport with the JPA persistence unit configuration so you shouldn't need hibernate.cfg.xml.

Vanquish39
Posts: 134
Posted 01:07 Oct 15, 2011 |

Thank you for the great explanation.  It helped me clear things up.

Also professor, I'm still a little confused on the merge/persist thing.  Was the merge only used to regain a copy of the original persisted object?  Also, how can I persist an object, go from a persisted to detached state, and still access the object's values?  Is that what merge is for?

cysun
Posts: 2935
Posted 08:11 Oct 15, 2011 |
Vanquish39 wrote:

Thank you for the great explanation.  It helped me clear things up.

Also professor, I'm still a little confused on the merge/persist thing.  Was the merge only used to regain a copy of the original persisted object?  Also, how can I persist an object, go from a persisted to detached state, and still access the object's values?  Is that what merge is for?

I don't quite understand your question. Basically in some circumstances there's a detached object, and then there's a managed copy loaded from the database. When you have two copies of an object (not just two references pointing to the same object), they can't magically become one, so merge() copies the states from the detached one to the managed one then saves it.

jonathankroening
Posts: 39
Posted 10:35 Feb 21, 2015 |
cysun wrote:
Vanquish39 wrote:

Thank you for the great explanation.  It helped me clear things up.

Also professor, I'm still a little confused on the merge/persist thing.  Was the merge only used to regain a copy of the original persisted object?  Also, how can I persist an object, go from a persisted to detached state, and still access the object's values?  Is that what merge is for?

I don't quite understand your question. Basically in some circumstances there's a detached object, and then there's a managed copy loaded from the database. When you have two copies of an object (not just two references pointing to the same object), they can't magically become one, so merge() copies the states from the detached one to the managed one then saves it.

I'm having an issue where merge() is trying to copy the state from the detached to the managed but throws an error that "Multiple representations of the same entity are being merged."  Managed and Detached.  Is this related to CascadeType?  I'm trying to update a set of CheckPoints in a ManyToMany relationship (updating from the "owner" side, in my case this is Student.)  Thanks!

cysun
Posts: 2935
Posted 12:22 Feb 21, 2015 |

It sounds like there are two detached copies of the same object. You should change your code to avoid situations like this. In particular, don't use session attributes unless it's really necessary.

jonathankroening
Posts: 39
Posted 12:33 Feb 21, 2015 |
cysun wrote:

It sounds like there are two detached copies of the same object. You should change your code to avoid situations like this. In particular, don't use session attributes unless it's really necessary.

Thank you.

Follow up:  I'm past that error now and on to a new one.

Found shared references to a collection: springmvc.model.Student.checked

Is this because I use models.put("checked"... ) when the view loads and then when performing POST I call Set<CheckPoint> checked = student.getCheckPoints()?  Is this the shared reference?  Am I meant to pass the prior "checked" as a param to POST?

cysun
Posts: 2935
Posted 13:10 Feb 21, 2015 |

I don't know what you are talking about.
 

jonathankroening
Posts: 39
Posted 14:24 Feb 21, 2015 |
cysun wrote:

I don't know what you are talking about.
 

Can you explain what this means then:  Found shared references to a collection: springmvc.model.Student.checked 

cysun
Posts: 2935
Posted 14:26 Feb 21, 2015 |
jonathankroening wrote:
cysun wrote:

I don't know what you are talking about.
 

Can you explain what this means then:  Found shared references to a collection: springmvc.model.Student.checked 

Based on the error message, it seems to mean that two objects (e.g. student1 and student2, or a managed student1 and a detached student1) are holding the same reference to a collection (i.e. checked).