休眠一对多注释映射 [英] Hibernate One to many Annotation Mapping

查看:49
本文介绍了休眠一对多注释映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类似下面的表格.

1)任务-ID,名称2)资源-id,名称,defaultTask(Task.id的外键)

映射是一对多的-一个任务可以拥有很多资源.

Task的代码如下.

  @Entity公共类Task实现了Serializable {私人长m_id;私有字符串m_name;@ID@GeneratedValue(策略= GenerationType.AUTO)公共长getId(){返回this.m_id;}公共无效setId(长id){this.m_id = id;}公共字符串getName(){返回this.m_name;}public void setName(String name){this.m_name =名称;}@OneToMany@JoinColumn(名称="defaultTask")私有列表<资源>m_relatedResources;公共列表<资源>getrelatedResources(){返回m_relatedResources;}public void setrelatedResources(List< Resource> relatedResources){m_relatedResources = relatedResources;} 

Resource类的代码如下.

  @Entity公共类Resource实现Serializable {私人Long m_id;私有字符串m_name;@ID@GeneratedValue(策略= GenerationType.AUTO)公共长getId(){返回this.m_id;}公共无效setId(长ID){this.m_id = id;}公共字符串getName(){返回this.m_name;}public void setName(String name){this.m_name =名称;}任务m_task;@多多@JoinColumn(名称="defaultTask")公共任务getTask(){返回this.m_task;}公共无效setTask(任务任务){this.m_task =任务;} 

}

当我执行它时,我会收到类似

的错误

初始SessionFactory创建失败.org.hibernate.MappingException:无法确定用于以下类型的类型:java.util.List,用于列:[org.hibernate.mapping.Column(relatedResources)] >

我做错了什么?如何解决该问题?

解决方案

您不能将注释随机应用于方法或字段.通常,您应该以与@Id相同的方式应用注释.

在Task类OneToMany中应该像

  @OneToMany@JoinColumn(名称="defaultTask")公共列表<资源>getrelatedResources(){返回m_relatedResources;} 

字段访问策略(由@Id注释确定).将每个JPA相关的注释直接放在每个方法的上方,而不是字段/属性,因为您的ID位于方法上方,这将使您摆脱表单异常.
@PredragMaric所引用的双向映射似乎也有问题,因此您需要使用MappedBy,这表明休眠的关系键在另一侧.单击映射.

.

Hi I have a two tables like below .

1) Task - id,name 2) Resource - id,name,defaultTask(foreign key to Task.id)

The mapping is one to Many - one task can have many resource.

The code for Task is like below.

@Entity
public class Task implements Serializable {
private long m_id;
private String m_name;

@Id
@GeneratedValue(
    strategy = GenerationType.AUTO
)
public long getId() {
    return this.m_id;
}

public void setId(long id) {
    this.m_id = id;
}

public String getName() {
    return this.m_name;
}

public void setName(String name) {
    this.m_name = name;
}

@OneToMany
@JoinColumn(
    name = "defaultTask"
)
private List<Resource> m_relatedResources;

public List<Resource> getrelatedResources() {
    return m_relatedResources;
}


public void setrelatedResources(List<Resource> relatedResources) {
    m_relatedResources = relatedResources;
}

And the code for Resource class is like below.

@Entity
public class Resource implements Serializable {
private Long m_id;
private String m_name;
@Id
@GeneratedValue(
    strategy = GenerationType.AUTO
)
public Long getId() {
    return this.m_id;
}

public void setId(Long id) {
    this.m_id = id;
}

public String getName() {
    return this.m_name;
}

public void setName(String name) {
    this.m_name = name;
}
Task m_task;
@ManyToOne
@JoinColumn(
    name = "defaultTask"
)
public Task getTask() {
    return this.m_task;
}

public void setTask(Task task) {
    this.m_task = task;
}

}

When i execute it I am getting an error like

Initial SessionFactory creation failed.org.hibernate.MappingException: Could not determine type for: java.util.List, for columns: [org.hibernate.mapping.Column(relatedResources)]

What have i done wrong ?How can i fix the problem ?

解决方案

You can't apply annotations to methods or fields randomly. Normally, you should apply your annotations the same way as @Id..

In Task class OneToMany should be like

@OneToMany
@JoinColumn(
    name = "defaultTask"
)
public List<Resource> getrelatedResources() {
    return m_relatedResources;
}

Field access strategy (determined by @Id annotation). Put any JPA related annotation right above each method instead of field / property as for your id it is above method and it will get you away form exception.
Also there appears to be an issue with your bidrectional mapping metntioned by @PredragMaric so you need to use MappedBy which signals hibernate that the key for the relationship is on the other side. Click for a really good question on Mapped by.

这篇关于休眠一对多注释映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆