i 'm trying the hibernate tutorials from their main site and wanted to change thing a bit to know how many to many relationship work with java.util.set interface.My mappings are correct and i can insert in and from the tables EVENT, PERSON and the mapping table PERSON_EVENT.Now i've inserted some dummy values in the tables and add their mappings in the mapping table.I wanted to display all the events of all the person who are register to an event or more. with this code :
我正在尝试从他们的主站点的hibernate教程,并希望稍微改变一下,以了解与java.util.set接口有多少关系。我的映射是正确的,我可以在表中插入事件,PERSON和映射表PERSON_EVENT.Now我已经在表中插入了一些虚拟值,并在映射表中添加了它们的映射。我想显示所有注册到事件或更多事件的人的所有事件。使用此代码:
public void ShowPersonEvents()
{
Person aperson;
Event anEvent;
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<Person> persons = session.createQuery("from Person").list();
for(int i =0; i< persons.size(); i++)
{
aperson = (Person) persons.get(i);
Set a = aperson.getEvents();
// String[] events = (String[])a.toArray(new String[a.size()]);
// for (String e : events)
// {
// System.out.println(aperson.getLastname()+" is registerd to the" + e);
//
// }
Iterator it = a.iterator();
while(it.hasNext())
{
System.out.println(aperson.getLastname()+" is registerd to the" +(String) it.next().toString());
}
// System.out.println();
}
session.getTransaction().commit();
}
}
so when i run is the who the correct number of rows but instead of show for example rows like :
所以当我运行的是谁是正确的行数而不是显示例如行如下:
Joseph is registered to the opensouce event
约瑟夫在opensouce活动中注册
it's rather showing something like :
它更像是这样的东西:
Joseph is registered to the domain.Event@18a8ce2
Joseph已注册域名.Event @ 18a8ce2
this is the format mypackagename.myclassname@something. when i comment the iterator part ant uncomment the casting to an string array i have an exception:arraystoreexception. I'm kind of lost a bit.I can't see what is wrong here.Please can you have a look and tell me what i did wrong?Thanks for reading.
这是格式mypackagename.myclassname@something。当我评论迭代器部分ant取消注释到一个字符串数组时,我有一个例外:arraystoreexception。我有点失落。我看不出这里有什么问题。请你看看我做错了什么?谢谢你的阅读。
3 个解决方案
#1
This doesn't really have anything to do with Hibernate. You're calling toString() on the Event object:
这与Hibernate没有任何关系。你在Event对象上调用toString():
(String) it.next().toString()
You haven't overridden the Event.toString() method, so you're getting the default implementation. Instead try something like:
您没有重写Event.toString()方法,因此您将获得默认实现。而是试着像:
while(it.hasNext()) {
Event event = (Event) it.next();
System.out.println(aperson.getLastname()+" is registerd to the" + event.getName());
}
You can also improve your Hibernate HQL query by pre-fetching the events. As it stands now they will be lazy loaded so you'll get an extra query for each person (assuming you haven't set the fetching strategy in the mapping file).
您还可以通过预先获取事件来改进Hibernate HQL查询。现在它们将被延迟加载,因此您将为每个人获得额外的查询(假设您没有在映射文件中设置提取策略)。
Try something like:
尝试类似的东西:
List<Person> persons = session.createQuery("from Person p left join fetch p.events").list();
#2
You haven't shown us your implementation of Person and Event class so I can only guess. To me it looks like you haven't overridden toString method in Event class, as the output looks like the result of the toString method inherited from Object
你没有向我们展示你的Person和Event类的实现,所以我只能猜测。对我来说,看起来你没有在Event类中重写toString方法,因为输出看起来像是继承自Object的toString方法的结果
#3
The reason you are seeing domain.Event@18a8ce2
is that this is the output from calling Object.toString()
(i.e. the default toString()
implementation). This implementation returns a String in the format @. If you wish to see the event's internal state you should override the toString() method in your Event class definition:
你看到domain.Event@18a8ce2的原因是这是调用Object.toString()的输出(即默认的toString()实现)。此实现以@格式返回String。如果您希望查看事件的内部状态,则应覆盖Event类定义中的toString()方法:
public String toString() {
return String.format("Event{ID: %d, Title: %s, Date: %s}", id, title, date);
}
The reason for the ArrayStoreException
is that you're trying to create a
String[]but are passing in objects that are not
Strings (they're Event
s). From the ArrayStoreException
Javadoc:
ArrayStoreException的原因是你正在尝试创建aString []但是传入的是notStrings(它们是事件)的对象。从ArrayStoreException Javadoc:
"Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects."
“抛出表示已尝试将错误类型的对象存储到对象数组中。”
So you need to create your Array by calling toArray(new Event[a.size()])
.
所以你需要通过调用toArray(new Event [a.size()]来创建你的Array。
#1
This doesn't really have anything to do with Hibernate. You're calling toString() on the Event object:
这与Hibernate没有任何关系。你在Event对象上调用toString():
(String) it.next().toString()
You haven't overridden the Event.toString() method, so you're getting the default implementation. Instead try something like:
您没有重写Event.toString()方法,因此您将获得默认实现。而是试着像:
while(it.hasNext()) {
Event event = (Event) it.next();
System.out.println(aperson.getLastname()+" is registerd to the" + event.getName());
}
You can also improve your Hibernate HQL query by pre-fetching the events. As it stands now they will be lazy loaded so you'll get an extra query for each person (assuming you haven't set the fetching strategy in the mapping file).
您还可以通过预先获取事件来改进Hibernate HQL查询。现在它们将被延迟加载,因此您将为每个人获得额外的查询(假设您没有在映射文件中设置提取策略)。
Try something like:
尝试类似的东西:
List<Person> persons = session.createQuery("from Person p left join fetch p.events").list();
#2
You haven't shown us your implementation of Person and Event class so I can only guess. To me it looks like you haven't overridden toString method in Event class, as the output looks like the result of the toString method inherited from Object
你没有向我们展示你的Person和Event类的实现,所以我只能猜测。对我来说,看起来你没有在Event类中重写toString方法,因为输出看起来像是继承自Object的toString方法的结果
#3
The reason you are seeing domain.Event@18a8ce2
is that this is the output from calling Object.toString()
(i.e. the default toString()
implementation). This implementation returns a String in the format @. If you wish to see the event's internal state you should override the toString() method in your Event class definition:
你看到domain.Event@18a8ce2的原因是这是调用Object.toString()的输出(即默认的toString()实现)。此实现以@格式返回String。如果您希望查看事件的内部状态,则应覆盖Event类定义中的toString()方法:
public String toString() {
return String.format("Event{ID: %d, Title: %s, Date: %s}", id, title, date);
}
The reason for the ArrayStoreException
is that you're trying to create a
String[]but are passing in objects that are not
Strings (they're Event
s). From the ArrayStoreException
Javadoc:
ArrayStoreException的原因是你正在尝试创建aString []但是传入的是notStrings(它们是事件)的对象。从ArrayStoreException Javadoc:
"Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects."
“抛出表示已尝试将错误类型的对象存储到对象数组中。”
So you need to create your Array by calling toArray(new Event[a.size()])
.
所以你需要通过调用toArray(new Event [a.size()]来创建你的Array。