简体   繁体   English

基本的JPA问题:“无法确定类型:java.util.Set”

[英]Basic JPA issue: “Could not determine type for: java.util.Set”

I'm just getting started building my JPA schema in a Play Framework web app. 我刚开始在Play Framework Web应用程序中构建我的JPA架构。 I have a reasonable understanding of SQL, but I'm a JPA newbie, and I'm being tripped up at the first hurdle. 我对SQL有一个合理的理解,但我是一个JPA新手,我在第一个障碍时被绊倒了。

From the Play tutorials I'm assuming you just create your Java classes and JPA/Play will automagically create the schema for you. 从Play教程我假设您只是创建Java类,JPA / Play将自动为您创建架构。

So I want to create a ManyToMany relationship between two model classes, Rankable and Tag: 所以我想在两个模型类(Rankable和Tag)之间创建一个ManyToMany关系:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Rankable extends Model {
    public String name;
    private Set<Tag> tags;

    @ManyToMany()
    @JoinTable(name = "RANKABLE_TAGS")
    public Set<Tag> getTags() {
        return tags;
    }

    @ManyToMany()
    @JoinTable(name = "RANKABLE_TAGS")
    public void setTags(final Set<Tag> tags) {
        this.tags = tags;
    }
}

And the other class: 而另一类:

@Entity
public class Tag extends Model {
    public String name;
    public String description;
    private Set<Rankable> rankables;

    @ManyToMany(mappedBy = "tags")
    public Set<Rankable> getRankables() {
        return rankables;
    }

    @ManyToMany(mappedBy = "tags")
    public void setRankables(final Set<Rankable> r) {
        rankables = r;
    }
}

But I keep getting the following error: 但我不断收到以下错误:

A JPA error occurred (Unable to build EntityManagerFactory): Could not determine type for: java.util.Set, at table: Rankable, for columns: [org.hibernate.mapping.Column(tags)] 发生JPA错误(无法构建EntityManagerFactory):无法确定类型:java.util.Set,在表:Rankable,对于列:[org.hibernate.mapping.Column(tags)]

What am I doing wrong? 我究竟做错了什么?

In our case, the reason was that we had some annotations on the field and some on the getters. 在我们的例子中,原因是我们在场上有一些注释,有些注释在吸气剂上。 In other words, for a specific field, the annotations should be either on the field or on the getter. 换句话说,对于特定字段,注释应该在字段上或在getter上。 Combining them on the getters solved the problem for us. 将它们结合在吸气剂上为我们解决了问题。 Seems like the exception do not show the real cause of the problem. 似乎异常没有显示问题的真正原因。 By the way, we used this syntax for the manytomany annotations: 顺便说一句,我们使用这种语法进行了许多注释:

  1. Entity: 实体:

     @ManyToMany @JoinTable(name = "join_table", joinColumns = { @JoinColumn(name = "leftjoinid") }, inverseJoinColumns = { @JoinColumn(name = "rightjoinid") }) 
  2. Entity: 实体:

     @ManyToMany @JoinTable(name = "join_table", joinColumns = { @JoinColumn(name = "rightjoinid") }, inverseJoinColumns = { @JoinColumn(name = "leftjoinid") }) 

In the end this seemed to work, although I'm not sure why: 最后这似乎有效,虽然我不确定为什么:

@Entity
public class Tag extends Model {
    public String name;
    public String description;

    @ManyToMany(mappedBy = "tags", cascade = CascadeType.ALL)
    public Set<Rankable> rankables;
}

and

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Rankable extends Model {
    @ManyToOne(cascade = CascadeType.ALL)
    public User creator;

    public String name;

    @ManyToMany()
    public Set<Tag> tags;
}

It looks like @ManyToMany doesn't take effect. 看起来@ManyToMany没有生效。

Perhaps it's some confusion with placement of annotations. 也许这与注释的放置有些混淆。 Make sure your getters have the corresponding setters ( setTags() ) and all other annotations are also placed on properties (not on fields). 确保你的getter有相应的setter( setTags() ),所有其他注释也放在属性上(不在字段上)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法确定:java.util.Set的类型,在表中 - Could not determine type for: java.util.Set, at table hibernate MappingException:无法确定类型:java.util.Set - hibernate MappingException: Could not determine type for: java.util.Set 多对多关系:org.hibernate.MappingException:无法确定以下类型:java.util.Set - many to many relationship : org.hibernate.MappingException: Could not determine type for: java.util.Set 使用@ElementCollection时出错:org.hibernate.MappingException:无法确定类型:java.util.Set,在表:对于列 - Error while using @ElementCollection: org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: for columns 无法构建 Hibernate SessionFactory; 嵌套异常是 org.hibernate.MappingException:无法确定类型:java.util.Set,在表中: - Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: org.hibernate.MappingException:无法确定类型:java.util.Set,在表:Company中,用于列:[org.hibernate.mapping.Column(users)] - org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: Company, for columns: [org.hibernate.mapping.Column(users)] java.util.Set,java.util.List可序列化的问题 - java.util.Set, java.util.List Serializable issue Sprint引导数据JPA:没有类型为'java.util.Set <javax.persistence.EntityManager>'的限定bean - Sprint Boot Data JPA: No qualifying bean of type 'java.util.Set<javax.persistence.EntityManager>' available 如何通过JPA持久化java.util.Set? - How to persist java.util.Set through JPA? MappingException:无法确定java.util.Set的类型 - MappingException: Type can not be determined for java.util.Set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM