Search code examples
javaspringhibernatespring-mvchibernate-mapping

Mapping Collection of basic types with hibernate


I'm trying to persist a List, however I'm getting this error

org.hibernate.MappingException: Could not determine type for: java.util.List, at table: Person, for columns: [org.hibernate.mapping.Column(comments)]

I've used the @ElementCollection annotation as specified in the hibernate documentation. I can't see what else to try.

Please ignore this paragraph is was only required so that i could submit the question.

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;




import org.hibernate.annotations.GenericGenerator;
import org.springframework.stereotype.Component;


@Component
@Entity
public class Person {
    private String name;

private Long id;
private String reviewName;
private String review;
private List<String> comments = new ArrayList<String>();


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy="increment")
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

@Column(columnDefinition="LONGTEXT")
public String getReview() {
    return review;
}

public void setReview(String review) {
    this.review = review;
}

public String getReviewName() {
    return reviewName;
}

public void setReviewName(String reviewName) {
    this.reviewName = reviewName;
}

public List<String> getComments() {
    return comments;
}

@ElementCollection
public void setComments(List<String> comments) {
    this.comments = comments;
}

public void addComment(String comment) {
    getComments().add(comment);
}

}


Solution

  • @elementcollection annotation should be put at getter (getComments()) instead of setter