org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.entity.annotations.House.district in com.entity.annotations.Street.houses
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:578)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:543)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:66)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1177)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:329)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1333)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at com.test.Demo04.saveHouseInfo(Demo04.java:53)
at com.test.Demo04.main(Demo04.java:18)
Exception in thread "main" java.lang.NullPointerException
at com.test.Demo04.saveHouseInfo(Demo04.java:74)
at com.test.Demo04.main(Demo04.java:18)
House.java
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_house")
@SequenceGenerator(name = "seq_house", sequenceName = "seq_house_id", allocationSize = 100, initialValue = 61)
private int id;
@Column(name = "title")
private String title; // 标题
@Column(name = "description")
private String description; // 描述
@Column(name = "price")
private int price; // 出租价格
@Column(name = "pubdate")
private Date pubdate; // 发布时间
@Column(name = "floorage")
private int floorage; // 面积
@Column(name = "contact")
private String contact; // 联系人
@Column(name = "user_id")
private int user_id; // 用户编号
@Column(name = "type_id")
private int type_id; // 类型编号
// private int street_id; // 街道编号
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "street_id")
private Street streets;
Street.java
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_street")
@SequenceGenerator(name = "seq_street", sequenceName = "seq_street_id", allocationSize = 100, initialValue = 11)
private int id;
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "district", cascade = { CascadeType.ALL })
private List<House> houses = new ArrayList<House>();
错误的原因是: @OneToMany(mappedBy = "district", cascade = { CascadeType.ALL }) 这里面的 mappedBy中“district”写错了映射的对象