第一次使用注解配置Hibernate,但是运行的时候出现了错误。
mappedBy reference an unknown target entity property
网上百度说这种错误是由于one方的mappedBy指定的不对,应该指定的是many方中的one方对象属性。但是我指定的是对的,但是不知道为什么就是错误的。
one方:
package com.bdqn.domain; import java.io.Serializable; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.SequenceGenerator; import javax.persistence.Table; @Entity @SequenceGenerator(name="user_seq",sequenceName="seq_sys_users") @Table(name="sys_users") public class Users implements Serializable{ private Integer usid; private String usname; private String uspwds; private Set lianxirens=new HashSet(0); //检查这个属性是否使用了泛型,如:Set<类型>. //如 果未使用泛型,请在OneToMany注释中使用targetEntity指定元素的类型 @Id @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="user_seq") //主键生成策略 取决于用什么数据库 public Integer getUsid() { return usid; } public void setUsid(Integer usid) { this.usid = usid; } public String getUsname() { return usname; } public void setUsname(String usname) { this.usname = usname; } @Column(name="uspwd") public String getUspwds() { return uspwds; } public void setUspwds(String uspwds) { this.uspwds = uspwds; } @OneToMany(cascade={CascadeType.ALL},fetch=FetchType.LAZY,mappedBy="user",targetEntity=Users.class) public Set getLianxirens() { return lianxirens; } public void setLianxirens(Set lianxirens) { this.lianxirens = lianxirens; } public Users(Integer usid, String usname, String uspwds, Set lianxirens) { super(); this.usid = usid; this.usname = usname; this.uspwds = uspwds; this.lianxirens = lianxirens; } public Users() { super(); } }
package com.bdqn.domain; import java.util.Date; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity @SequenceGenerator(name="lianxiren_seq",sequenceName="seq_lianxiren") @Table(name="lianxiren") public class Lianxirens { private Integer lid; private String lname; private String phone; private Date times; private Users user; @Id @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="lianxiren_seq") public Integer getLid() { return lid; } public void setLid(Integer lid) { this.lid = lid; } public String getLname() { return lname; } public void setLname(String lname) { this.lname = lname; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Column(nullable=true) @Temporal(TemporalType.DATE) //时间类型 public Date getTimes() { return times; } public void setTimes(Date times) { this.times = times; } @ManyToOne(cascade={CascadeType.ALL},fetch=FetchType.EAGER,optional=true) @JoinColumn(name="usid",insertable=true,updatable=true) public Users getUser() { return user; } public void setUser(Users user) { this.user = user; } public Lianxirens(Integer lid, String lname, String phone, Date times, Users user) { super(); this.lid = lid; this.lname = lname; this.phone = phone; this.times = times; this.user = user; } public Lianxirens() { super(); } }
请高手看一下,错误出现在了哪?
联系人表中的外键列也叫usid么回复<aclass='referer'target='_blank'>@_inDesperation:总之还是得谢谢你!回复<aclass='referer'target='_blank'>@回忆的沙漏:hibernate用注解是比较容易混,建议不要使用相同的变量名嗯是的呢~~~纠结了很久还是没能出来结果hibernate中的<spanstyle="font-family:Consolas,'BitstreamVeraSansMono','CourierNew',Courier,monospace;line-height:14.65625px;background-color:#FFFFFF;">mappedBy要填写bean中set后的几个字符,<spanstyle="background-color:#F8F8F8;"> 而不是属性列表的名称。 比如你这个应该是<spanstyle="background-color:#F8F8F8;">setUser中的“User”,不是“user”NO,我改了,还是报那个错误。。。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。