-->
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.  [ 4 posts ] 
Author Message
 Post subject: Parent/child relationship
PostPosted: Tue Jan 29, 2008 6:52 pm 
Newbie

Joined: Tue Jan 29, 2008 6:37 pm
Posts: 9
I'm trying to implement a more complex version of the Parent/child relationship that is explained in the Hibernate documentation.

I have the following annotated classes:

Code:
// Parent class
@Entity
@Table(name="albums")
public class Album implements Serializable {
   private static final long serialVersionUID = -4143935150417416554L;
   
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private Long id;
   private String name;
   private String description;
   
   @Column(name="creation_date", nullable=false)
   private Date creationDate;
   
   @Column(name="picture_count")
   private Integer pictureCount;
   
   @OneToOne(cascade=CascadeType.ALL)
   @JoinColumn(name="thumbnail")
   private Picture thumbnail;
   
   @OneToMany(mappedBy="album", cascade=CascadeType.ALL)
   @JoinColumn(name="album_id")
   private Set<Picture> pictures;
     
        // getters and setters ...
}

// Child class
@Entity
@Table(name="pictures")
public class Picture implements Serializable {
   private static final long serialVersionUID = 545720237908797515L;
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private Long id;
   
   @Column(name="path", nullable=false)
   private String path;
   private String thumbnail;
   @Column(name="creation_date")
   private Date creationDate;
   private String description;
   private String name;
   
   @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
   private Album album;
   
   @Column(name="content_type")
   private String contentType;
   private Integer width;
   private Integer height;
   
   @Column(name="image_size")
   private Integer size;

        // getters & setters
}


My problem is that when creating the albums, and for each album creating its pictures, I want the first picture to be set as thumbnail for the album.
However Hibernate does not seem to do this, he inserts the album and every picture but the final update is missing.

Currently I have the following code:
Code:
Album album = albumDao.findByName(name);
if (album == null) { // create new one
       album = new Album();
       // set properties
}

int i = 0;
Set<Picture> pics = new HashSet<Picture>();
for(File picture : pictures) {
      Picture newPicture = new Picture();
      // set properties
      newPicture.setAlbum(album);
      pictureDao.save(newPicture);
      if (i == 0)
           album.setThumbnail(newPicture);
}
album.setPictures(pics);
albumDao.save(album);


My tables are created as follows:

Code:
create table albums(
   id int(11) not null auto_increment,
   name varchar(100),
   creation_date date not null,
   description varchar(1000),
   picture_count int(8) default 0,
   thumbnail int(11),
   constraint pk_albums primary key(id)
);

create table pictures(
   id int(11) not null auto_increment,
   path varchar(1000) not null,
   description varchar(1000),
   creation_date date not null,
   album_id int(11) not null,
   thumbnail varchar(1000),
   name varchar(100),
   content_type varchar(50),
   width int(8),
   height int(8),
   thumb_width int(8),
   thumb_height int(8),
   image_size int(11),
   constraint pk_pictures primary key(id),
   constraint fk_pictures_album foreign key(album_id) references albums(id)
);


I find it strange that I need to tell hibernate explicitly to save each picture, normally I'd expect him to guess the correct order to be
insert parent
insert all children
update parent


I use Hibernate 3.2.5 with the annotations 3.3.0.GA, MySQL 5.0.44, Java 1.5.0.14 and JBoss 4.2.1

I wonder what I'm doing wrong here though, the whole thing is running within a read/write transaction using Spring's transactional annotation, no problems there.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 03, 2008 3:24 pm 
Newbie

Joined: Tue Jan 29, 2008 6:37 pm
Posts: 9
is there not enough information in my question, has this question been asked too much or is this truly a shortcoming in Hibernate? :s


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 03, 2008 4:36 pm 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
Hi,

I think there is a mistake in your annotations. In Album.java, the following Set mapping will work fine without the @JoinColumn. If you think about it, hibernate has enough information without it: the name of the class (from Set<Picture> and the property that refers back to this Album (mappedBy="album")

Code:
   @OneToMany(mappedBy="album", cascade=CascadeType.ALL)
   @JoinColumn(name="album_id") // NOT NEEDED!
   private Set<Picture> pictures;


More importantly, you DO need @JoinColumn(name="album_id") in Picture.java on the reference back to the parent Album. Again, think about what you had before, hibernate doesn't have any information about a Picture's Album - the @JoinColumn tells it that album_id is a foreign key to Album.

Code:
   @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
   @JoinColumn(name="album_id")
   private Album album;


If you fix that, I would expect your code fragment to work as you intended.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 04, 2008 7:01 am 
Newbie

Joined: Tue Jan 29, 2008 6:37 pm
Posts: 9
Well, almost actually, it saves the album and all pictures and even sets the thumbnail but that one picture that is the thumbnail has no album_id filled in.

That being said; here is the executed sql:

Code:
11:59:55,526 INFO  [STDOUT] Hibernate: insert into pictures (album_id, content_type, creation_date, description, height, name, path, image_size, thumb
nail, width) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
11:59:55,698 INFO  [STDOUT] Hibernate: insert into albums (creation_date, description, name, picture_count, thumbnail) values (?, ?, ?, ?, ?)
11:59:55,808 INFO  [STDOUT] Hibernate: insert into pictures (album_id, content_type, creation_date, description, height, name, path, image_size, thumb
nail, width) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
11:59:55,854 INFO  [STDOUT] Hibernate: insert into pictures (album_id, content_type, creation_date, description, height, name, path, image_size, thumb
nail, width) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
11:59:55,901 INFO  [STDOUT] Hibernate: insert into pictures (album_id, content_type, creation_date, description, height, name, path, image_size, thumb
nail, width) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
11:59:55,964 INFO  [STDOUT] Hibernate: insert into pictures (album_id, content_type, creation_date, description, height, name, path, image_size, thumb
nail, width) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)


He forgets to do an update at the end :(


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.