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;
}
我遇到以下运行时异常:无法读取首选项的注释
我错过了什么
谢谢 您在这段代码中犯了几个错误:
@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;
}
@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);
}