File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Problem with creating db - what's wrong? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problem with creating db - what Watch "Problem with creating db - what New topic
Author

Problem with creating db - what's wrong?

Paul Silesian
Greenhorn

Joined: Aug 02, 2011
Posts: 4
Hi all,


I'm new here .

First info: I'm using:
Hibernate Annotations 3.4.0.GA
Hibernate 3.3.0.SP1

I need help about creating properly structure of db. Probably I don't good understand or I did mistake. I have some entities (File, Music, Playout, Playout_Files, Playout_Music) and I want that one object Playout have some File's and Music's for example:



Everything is OK, next I want add new entity Playout, example:



I've got error:



I understand that we cannot use the same item File and/or Music used in first Playout.

I tried use (the same for Music)



But it looks like wrong, I cannot explain it good..

How should I change structure/mapping that I can use again used item File?

Is it possible do it?



Below full code and some data examples:




Below file Playout.java



Below file File.java



Below file Music.java




Best regards,
Paul
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I think you're telling us half of the truth. In your example you invoke the constructor: new Playout(2, 'Title2', files, musics)
Yet the class Playout only has the constructors: public Playout() & public Playout(String filetitle, Set<File> files, Set<Music> musics)

Maybe a typo.

Also why do you initialize musics but not versionFiles in Playout?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

And welcome to the JavaRanch!
Paul Silesian
Greenhorn

Joined: Aug 02, 2011
Posts: 4
Hi,

Thank you for reply. You are right.. I said half of the truth.. I wrote it from member, Some code I was simplied for readers. I'll put whole methods:



I fixed code in Playout.java, initialization at begin code:



Eclipse recompiled whole code, next deploy on tomcat.
I started browser, clicked first time (I selected first Music, first File, entered name title of Playout and time)
and I see that it saved into db. Next I put new name title of Playout and time, selected again first Music, first File. And I've got error that:




When I selected second file from tableFile, second music from tableMusic it wrote successfully.

The problem in my opinion is in wrong structure that I cannot save the same Music/File used again as new entity Playout. Any ideas?


Best regards,
Paul Silesian

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

What is the constraint name that gets violated (see the exception)?
Paul Silesian
Greenhorn

Joined: Aug 02, 2011
Posts: 4
I see that constraint violated is PLAYOUT_MUSIC. And I understand that it has unique fields - it means that I cannot add music with ID used earlier.

To be preciselly: playout_music has 2 fields: playoutId, musicId. the both are unique. Example:

playoutId, musicId
1,1
1,2
1,3

For me solution is: add next field unique like that: playoutmusicId.


How can I do it?


The same situation is for playout_files. I cannot add file with ID used earlier.

If I think wrong, please correct me.


Best regards,
Paul Silesian
Paul Silesian
Greenhorn

Joined: Aug 02, 2011
Posts: 4
OK, I'm read again book about Hibernate and I see that I did one mistake. It should be mapping @ManyToMany because entity File is used more than one in various entities Playout. The same is for entity Music.

Below solution:

in Playout.java




Best regards,
Paul Silesian
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with creating db - what's wrong?