Search code examples
hibernatejpajpa-2.0hibernate-mapping

Error while saving Hibernate Inheritance


My application is trying to save a Project object, and a Photo object which is a Media. Photo table has media_id as foreign key in it.

My classes are

Project.java

@Entity
@Table(name = "tbl_project", catalog = "chetak")
public class Project implements java.io.Serializable {

private Long projectId;
private String projectName;
private Set<Media> projectMedias = new HashSet<Media>(0);

@OneToMany(fetch = FetchType.EAGER)
@Cascade({CascadeType.SAVE_UPDATE})
public Set<Media> getProjectMedias() {
    return this.projectMedias;
}

public void setProjectMedias(Set<Media> projectMedias) {
    this.projectMedias = projectMedias;
}

Media.java

@Entity
@Table(name = "tbl_media", catalog = "chetak")
@Inheritance(strategy=InheritanceType.JOINED)
public class Media implements java.io.Serializable {

private Integer mediaId;
private String type;
private String mime;

Photo.java

@Entity
@Table(name = "tbl_photo", catalog = "chetak")
@PrimaryKeyJoinColumn(name="mediaId")
public class Photo extends Media implements java.io.Serializable {

//private Integer photoId;
private Integer height;
private Integer width;
private String photoType;

In the main class, I am adding a photo to the set of media in a project.

Photo photo = new Photo();
photo.setPhotoType("logo");
photo.setWidth(100);
photo.setHeight(100);
project.getProjectMedias().add(photo);

On saving the project object, I am getting the following log. See at the the end there is an unwanted insert to a non existing table which is creating the error:

Hibernate: insert into chetak.tbl_project (addressId, availableUnits, basicSellingPrice, bhk, builtUpAreaHigh, builtUpAreaLow, category, contactId, currentProjectStatus, description, developer, endPrice, latitude, listingTitle, listingType, longitude, numberOfBuildings, postedByName, postedByUserId, postedByUserType, projectLaunchDate, projectName, sourceListedDate, startPrice, totalUnits) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into chetak.tbl_media (caption, dateCreated, description, isApproved, mime, size, source, title, type, url) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into chetak.tbl_photo (height, photoType, width, mediaId) values (?, ?, ?, ?)

Hibernate: insert into tbl_project_tbl_media (tbl_project_projectId, projectMedias_mediaId) values (?, ?)
WARN : org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1146, SQLState: 42S02
ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Table 'chetak.tbl_project_tbl_media' doesn't exist

Is this due to some hibernate mapping error?


Solution

  • Hibernate creates a link table named tbl_project_tbl_media for your one-to-many relationship. I think you just want a column in your Media table referring to the project, thus avoiding the use of a link table.

    The @JoinColumn annotation does just that. I can't compile and run it myself to test right now, so not going to post any code, but basically you can create a Project field in your Media class, and then use @JoinColumn("yourColumnName") right before or after @OneToMany.

    See this question: @OneToMany without inverse relationship and without a join table?