Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 使用ebean进行外键约束_Hibernate_Orm_Playframework_Ebean - Fatal编程技术网

Hibernate 使用ebean进行外键约束

Hibernate 使用ebean进行外键约束,hibernate,orm,playframework,ebean,Hibernate,Orm,Playframework,Ebean,表: 属性:电影属性(戏剧、惊悚片等)(包含列:id、名称) User:系统中的用户(包含列:id、name) Movie:具有相关属性(如“矩阵”具有“科幻-60%”、“颤栗-40%”等)(具有列:id、attrid、fraction)attrid是属性的外键 首选项:用户对电影的首选项(有列:id、uid、movieid、rating)uid是用户的外键,movieid是电影的外键 这是我到目前为止的模型描述: User: @Entity @Table(name="USER")

表:

  • 属性
    :电影属性(戏剧、惊悚片等)(包含列:id、名称)
  • User
    :系统中的用户(包含列:id、name)
  • Movie
    :具有相关属性(如“矩阵”具有“科幻-60%”、“颤栗-40%”等)(具有列:id、attrid、fraction)attrid是属性的外键
  • 首选项
    :用户对电影的首选项(有列:id、uid、movieid、rating)
    uid
    用户
    的外键,
    movieid
    电影
    的外键
  • 这是我到目前为止的模型描述:

      User:
    
      @Entity
      @Table(name="USER")
      public class User extends Model {
        @Id
        @Column(name = "uid")
        public Long uid;
    
        @Column(name = "name")
        public String name;
      }
    
      Movie Attribute:
    
      @Entity
      @Table(name = "MOVIEATTRIBUTE")
      public class Attribute extends Model {
        @Id
        @Column(name = "attrid")
        public Long attrid;
    
        @Column(name = "name")
        public String name;
    
        @ManyToMany
        @JoinColumn(name = "movieid")
        public Movie movie;
      }
    
      Movie:
    
      @Entity
      public class Movie extends Model {
        @Id
        @Column(name = "movieid")
        public Long movieid;
    
        @ManyToMany
        @JoinColumn(name = "attrid")
        public Attribute attribute;
    
        @Column(name = "rating")
        public Integer rating;
      }
    
      Preference:
    
      @Entity
      public class Preference extends Model {
        @Id
        @Column(name = "prefid")
        public Long prefid;
    
        @ManyToMany
        @JoinColumn(name="uid")
        public User user;
    
        @ManyToMany
        @JoinColumn(name="movieid")
        private Movie movie;
    
        @Column(name = "rating")
        public Integer rating;
      }
    
    我遇到以下运行时异常:无法读取首选项的注释

    我错过了什么


    谢谢

    您在这段代码中犯了几个错误:

  • 您错误地使用了@ManyToMany注释。您已将其添加到属性类中的“电影电影”字段和电影类中的“属性属性”字段中。 因此,只能将一个属性指定给电影对象,将一个电影指定给属性。但是注释说,单个电影可以有许多属性,属性可以有许多电影。 如果使用@ManyToMany或@OneToMany注释,则应使用集合。因此,这里的适当关系应该是:

    电影课:

    @Entity
    public class Movie extends Model {
        @Id
        public Long id;
    
        @OneToMany(mappedBy="movie")
        public List<Attribute> attributes;
    
        public Integer rating;
    
        public String title;
    }
    
    @OneToMany(mappedBy=“电影”) 公共列表属性

  • 在属性类中:

    @ManyToOne
    public Movie movie;
    
    @Entity
    public class Attribute extends Model {
        @Id
        public Long id;
    
        public String name;
    
        @ManyToOne
        public Movie movie;
    }
    
  • 在定义两个模型类之间的双向关系时(在两个类中都放置注释),应该在其中一个类中使用“mappedBy”属性。 此属性表示第二个类中的字段,它是此关系的第二个端点
  • 你到处使用@manytoman关系。但从你的描述我推断你不需要这种关系。您需要两个双向@ManyToOne关系和一个单向@OneToOne关系
  • 您的电影类没有“title”属性
  • 您不必为表和列指定名称。如果省略此项,它们将与类名和字段名相同

    我对您的代码进行了一些更正,以使其正常工作:

  • 用户类别:

    @Entity
    public class User extends Model {
        @Id
        public Long id;
    
        public String name;
    
        @OneToMany(mappedBy="user")
        public List<Preference> preferences;    
    }
    
    首选项类别:

    @Entity
    public class Preference extends Model {
        @Id
        public Long id;
    
        @ManyToOne
        public User user;
    
        @OneToOne
        public Movie movie;
    
        public Integer rating;
    }
    
    试验方法:

    @Test
    public void movieTest () {
        FakeApplication app = Helpers.fakeApplication(Helpers.inMemoryDatabase());
        Helpers.start(app);
    
        User u = new User();
        u.id=1L;
        u.name="John";
    
        Movie m = new Movie();
        m.id = 1L;
        m.title = "Matrix";
        m.rating = 5;
    
        Attribute a = new Attribute();
        a.id = 1L;
        a.name = "Comedy";
        a.movie = m;
        m.attributes.add(a);
    
        Preference p = new Preference();
        p.id = 1L;
        p.rating = 10;
        p.user=u;
        p.movie=m;
    
        Ebean.save(u);
        Ebean.save(m);
        Ebean.save(a);      
        Ebean.save(p);
    
        User fu = Ebean.find(User.class, 1L);
        Movie fm = Ebean.find(Movie.class, 1L);
        Attribute fa = Ebean.find(Attribute.class, 1L);
        Preference fp = Ebean.find(Preference.class, 1L);
    
        System.out.println("User: id:"+fu.id+" name:"+fu.name+ " preference_rating:"+fu.preferences.get(0).rating);
        System.out.println("Movie:  id:" + fm.id+" rating:" + fm.rating + " attrname:"+fm.attributes.get(0).name);
        System.out.println("Attribute:  id:"+fa.id+" name:"+fa.name);
        System.out.println("Preference:  id:"+fp.id+" name:"+fp.rating+" username:"+fp.user.name+" movietitle:"+fp.movie.title);
    }