有没有一种方法映射ListProperties

时间:2018-05-12 02:29:39

标签: java hibernate jpa mapping

我正在尝试使用JPA Hibernate映射类的ListProperties属性。我经常搜索并决定在这里提出这个问题。我是新用的JPA和Hibernate,如果我违反规则,对不起,我抓住了机会。

有没有办法映射和使用ListProperties?如果你想知道为什么我不使用List而不是List,我有一个项目,我需要用JPA和Hibernate实现数据库控件。此外,这个项目是用maven实现的,使用mariaDB是必须的。

Student.class

@Entity
@Table(name = "student")

public class Student{

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@NotNull
private int id;

@Column(name = "text")
@NotNull
private String text;

@ElementCollection
@JoinColumn(name = "s_no")
private ListProperty<String> sList;

public Student(String text) {

    this.text = text;
    sList = new SimpleListProperty<String>();
}

public int getId() {
    return id;
}

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

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public ObservableList<String> getsList() {
    return sList.get();
}

public ListProperty<String> sListProperty() {
    return sList;
}

public void setsList(ObservableList<String> sList) {
    this.sList.set(sList);
}

我运行

时出现以下错误
Exception in thread "main" org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: model.Student.sList
at org.hibernate.cfg.annotations.CollectionBinder.getCollectionBinder(CollectionBinder.java:324)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1899)
at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:913)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:740)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:249)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:858)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:885)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at controller.MainApp.main(MainApp.java:24)

0 个答案:

没有答案