开发者社区> 问答> 正文

全注解配置Hibernate报错!在线等!!!急?报错

第一次使用注解配置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();
		}
}



many方:
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();
		}
		
}



one方是用户表,many方是联系人表,一个用户对应多个联系人

请高手看一下,错误出现在了哪?

展开
收起
爱吃鱼的程序员 2020-06-22 11:28:24 474 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    联系人表中的外键列也叫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,我改了,还是报那个错误。。。

    2020-06-22 11:28:41
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载