The type of field isn't supported by declared persistence strategy “OneToMany”

荒凉一梦 提交于 2019-12-04 08:11:38

When you are working with JPA, you should think Object and relations between Objects and you should map your Object model, not ids, to your relational model (it is possible to map a List of basic values with @ElementCollection in JPA 2.0 though but what I said just before still applies).

Here, (assuming this really is a one-to-many relation between Message and GroupAssoc and not a many-to-many relation between Message and Group entities) you should have something like this:

@Entity
@Table(name = "MESSAGE")
public class Message implements Serializable {
    @Id
    @Column(name = "MESSAGE_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)    
    private Long messageId;

    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.PERSIST)    
    private List<GroupAssoc> groupAssocs = new ArrayList<GroupAssoc>();

    public Long getMessageId() {
        return messageId;
    }
    public void setMessageId(Long messageId) {
        this.messageId = messageId;
    }

    public List<GroupAssoc> getGroupAssocs() {
        return groupAssocs;
    }
    public void setGroupAssocs(List<GroupAssoc> groupAssocs) {
        this.groupAssocs = groupAssocs;
    }

    // equals() and hashCode()
}

And another entity for GroupAssoc.

PS: Your DDL really looks like a (M:N) relation between MESSAGE and GROUP (or I don't understand the PK constraint of GROUP_ASSOC) but you didn't show any FK constraint on GROUP_ID so I'm not 100% sure. But if that's the case, then you should use an @ManyToMany instead of @OneToMany.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!