一对多关系和其他对象关系 [英] One to many relationship and Other object relation

查看:191
本文介绍了一对多关系和其他对象关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学习hibernate。这可能是一个基本问题,我无法弄清楚这个问题,

我有一个叫做 Customer 的对象,它会有与地址对象(一对一关系)门票对象(一对多关系) STRONG>。 [每位顾客可以有许多票> 。并且每张门票都有一个付款对象一对一关系)。我在hibernate注释中指定了关系。



执行测试时出现以下错误,

 初始化SessionFactory创建failed.org.hibernate.AnnotationException:未知mappedBy in:com.test.model.Tickets.payment,引用属性unknown:com.test.model.Payment.tickets 
线程mainjava中的异常。 lang.ExceptionInInitializerError

如果我在java中删除了 Payment对象及其引用以及 hibernate.cfg.xml 中的对三个对象(客户,地址和票据)均可正常工作。如果为客户添加付款对象,它不起作用。我在哪里错了这个代码?



我的对象,
客户:

地址:





门票:





付款方式:



$ b 测试程序是:


Hibernate Configuration是:

解决方案

正如在错误消息中所说的,在 Payment 中没有名为票据的属性。取而代之的是名为 ticket (单数)的属性。应该使用以下内容:

  mappedBy =ticket


I am learning hibernate. It may be a basic question, I am not able to figure out the issue,

I am having an object called Customer, It will have a relationship with address object (one-to-one relation) and Tickets object (one-to-many relation). [Each customer can have many tickets]. And Each ticket will have a payment object (One-to-one relation). I specified the relation in hibernate annotation.

I am getting the below error when executing the test,

Initial SessionFactory creation failed.org.hibernate.AnnotationException: Unknown mappedBy in: com.test.model.Tickets.payment, referenced property unknown: com.test.model.Payment.tickets
Exception in thread "main" java.lang.ExceptionInInitializerError

If I remove the Payment object and their reference in java as well as in hibernate.cfg.xml, It works fine with three objects (Customer, Address, and Tickets). If add a Payment object for the customer , It is not working. Where am i wrong in this code ?

My objects, Customer :

Address :

Tickets :

Payment :

Test program is :

Hibernate Configuration is :

解决方案

As said in the error message, there is no property named tickets in Payment. Instead of it there is property named ticket (singular). Following should be used:

mappedBy="ticket"

这篇关于一对多关系和其他对象关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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