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

时间:2022-01-11 19:45:09

I'm just getting started building my JPA schema in a Play Framework web app. I have a reasonable understanding of SQL, but I'm a JPA newbie, and I'm being tripped up at the first hurdle.

我刚开始在Play Framework Web应用程序中构建我的JPA架构。我对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?

我究竟做错了什么?

3 个解决方案

#1


7  

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. 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:

在我们的例子中,原因是我们在场上有一些注释,有些注释在吸气剂上。换句话说,对于特定字段,注释应该在字段上或在getter上。将它们结合在吸气剂上为我们解决了问题。似乎异常没有显示问题的真正原因。顺便说一下,我们将这种语法用于许多注释:

  1. Entity:

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

  3. Entity:

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

#2


2  

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;
}

#3


0  

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()),所有其他注释也放在属性上(不在字段上)。

#1


7  

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. 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:

在我们的例子中,原因是我们在场上有一些注释,有些注释在吸气剂上。换句话说,对于特定字段,注释应该在字段上或在getter上。将它们结合在吸气剂上为我们解决了问题。似乎异常没有显示问题的真正原因。顺便说一下,我们将这种语法用于许多注释:

  1. Entity:

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

  3. Entity:

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

#2


2  

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;
}

#3


0  

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()),所有其他注释也放在属性上(不在字段上)。