4

When Hibernate trying create mapping for the Item the next Exception throws http://pastebin.com/RdysJeiU

The cause of the exception is mismatch between column number and actual properties number.

The next is content of arrays:

columnAliases = [IMAGE_TITLE]
propertyNames = [imageMetadata,title]

-

org.hibernate.persister.collection;

public abstract class AbstractCollectionPersister

private void initCollectionPropertyMap(String aliasName, Type type, String[] columnAliases, String[] columnNames) {

    collectionPropertyColumnAliases.put( aliasName, columnAliases );
    collectionPropertyColumnNames.put( aliasName, columnNames );

    if ( type.isComponentType() ) {
        CompositeType ct = (CompositeType) type;
        String[] propertyNames = ct.getPropertyNames();
        for ( int i = 0; i < propertyNames.length; i++ ) {
            String name = propertyNames[i];
            collectionPropertyColumnAliases.put( aliasName + "." + name, columnAliases[i] );
            collectionPropertyColumnNames.put( aliasName + "." + name, columnNames[i] );
        }
    }

}

The mapping of the Photo entity complete without error.

Why Hibernate can't map the imageMetadata property of the @Embeddable Image class of the @Embeddable collection property of the Item entity?

package model.dao.mappings; import javax.persistence.*;

@MappedSuperclass
public class BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
protected long id;

public BaseEntity() {}

public long getId() {
    return id;
}
}

package model.dao.mappings.collections.embedable;
import model.dao.mappings.BaseEntity;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Item extends BaseEntity {

@Column(name = "ITEM_NAME")
private String name;

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "ITEM_IMAGE", joinColumns = @JoinColumn(name = "ITEM_ID"))
private List<Image> images = new ArrayList<Image>();

private Item() {}

public Item(String name, List<Image> images) {
    this.name = name;
    this.images = images;
}

public List<Image> getImages() {
    return images;
}

public void setImages(List<Image> images) {
    this.images = images;
}

}

package model.dao.mappings.collections.embedable;
import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Image {

@Column(name = "IMAGE_TITLE")
private String title;

private ImageMetadata imageMetadata;

private Image() {}

public Image(String title, ImageMetadata imageMetadata) {
    this.title = title;
    this.imageMetadata = imageMetadata;
}
}

package model.dao.mappings.collections.embedable;
import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class ImageMetadata {

@Column(name = "SIZE_X")
private int sizeX;

@Column(name = "SIZE_Y")
private int sizeY;

private ImageMetadata() {}

public ImageMetadata(int sizeX, int sizeY) {
    this.sizeX = sizeX;
    this.sizeY = sizeY;
}
}

package model.dao.mappings.collections.embedable;
import model.dao.mappings.BaseEntity;
import javax.persistence.*;
import java.util.List;
@Entity
public class Photo extends BaseEntity {
private String title;

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "PHOTO_METADATA", joinColumns = @JoinColumn(name = "PHOTO_ID"))
private List<ImageMetadata> metadataList;

private Photo() {}

public Photo(String title, List<ImageMetadata> metadataList) {
    this.title = title;
    this.metadataList = metadataList;
}
}

1 Answer 1

6

Problem solves if I add getters/setters. However, I still don't understand why I need to do this and why for Photo entity mapping works without accessors. According to "Java Persistence with Hibernate" (authors are the Hibernate developers) second edition " This is a rule in JPA: if @Id is on a field, the JPA provider will access fields of the class directly" The BaseEntity use @Id on a field, so access type is the FIELD, not PROPERTY. I don't understand why Hibernate can't use reflection for nesting embedded object's when access type is FIELD.

The problem also solves if I add @Access(AccessType.FIELD) annotation to @Embeddable ImageData.

Summary

According to JPA specification

2.3.1 Default Access Type

By default, a single access type (field or property access) applies to an entity hierarchy. The default access type of an entity hierarchy is determined by the placement of mapping annotations on the attributes of the entity classes and mapped superclasses of the entity hierarchy that do not explicitly specify an access type. An access type is explicitly specified by means of the Access annotation [6], as described in section 2.3.2

It's not work for the nesting @Embeddable classes (more than one), if you using the Hibernate. For the proper mapping you need to explicitly specify the access type on @Embeddable classes.

1
  • Looks like Hibernate bug, more detail here
    – senleft
    Dec 23, 2013 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.