简体   繁体   English

休眠和Java.util.set问题

[英]hibernate and Java.util.set problem

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. 我正在尝试从其主站点进行休眠教程,并且想做些改动以了解java.util.set接口之间存在多少关系。我的映射是正确的,我可以在表EVENT,PERSON中插入和插入现在,我已经在表中插入了一些虚拟值,并将它们的映射添加到映射表中。我想显示所有已注册某个事件或更多事件的人的所有事件。 with this code : 使用此代码:

 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 约瑟夫(Joseph)注册参加了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. 这是格式mypackagename.myclassname@something。 when i comment the iterator part ant uncomment the casting to an string array i have an exception:arraystoreexception. 当我注释迭代器部分ant并将注释转换为字符串数组时,我有一个异常: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. 我有点迷茫。我看不到这里有什么问题。请您看看我做错了什么吗?谢谢阅读。

This doesn't really have anything to do with Hibernate. 这实际上与Hibernate没有任何关系。 You're calling toString() on the Event object: 您正在事件对象上调用toString():

(String) it.next().toString()

You haven't overridden the Event.toString() method, so you're getting the default implementation. 您尚未重写Event.toString()方法,因此将获得默认实现。 Instead try something like: 而是尝试类似:

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. 您还可以通过预提取事件来改进Hibernate HQL查询。 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). 现在,它们将被延迟加载,因此您将为每个人获得一个额外的查询(假设您尚未在映射文件中设置获取策略)。

Try something like: 尝试类似:

List<Person> persons = session.createQuery("from Person p left join fetch p.events").list();

The reason you are seeing domain.Event@18a8ce2 is that this is the output from calling Object.toString() (ie the default toString() implementation). 您看到domain.Event@18a8ce2的原因是这是调用Object.toString()的输出(即默认的toString()实现)。 This implementation returns a String in the format @ . 此实现以@格式返回String。 If you wish to see the event's internal state you should override the toString() method in your Event class definition: 如果希望查看事件的内部状态,则应在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 yo u're trying to create a String[] but are passing in objects that are not String s (they're Event s). ArrayStoreException的原因是,您u're trying to create a String [], but are passing in objects that are not String s (they're Event but are passing in objects that are not s (they're Event )。 From the ArrayStoreException Javadoc: 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()])创建数组。

You haven't shown us your implementation of Person and Event class so I can only guess. 您尚未向我们展示您的PersonEvent类的实现,所以我只能猜测。 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 对我来说,您似乎没有在Event类中重写toString方法,因为输出看起来像是从Object继承的toString方法的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 hibernate MappingException:无法确定类型:java.util.Set - hibernate MappingException: Could not determine type for: java.util.Set Hibernate: How to map java.util.Map with java.util.Set as a value with LocalDate inside? - Hibernate: How to map java.util.Map with java.util.Set as a value with LocalDate inside? JPA + Hibernate将一个实体中的java.util.Set映射到另一个实体java.util.Map - JPA + Hibernate mapping java.util.Set in one Entity to another Entity java.util.Map 用`_`定义java.util.Set的读取 - Defining Reads for java.util.Set with `_` java.util.Set,但对字段具有自定义约束 - java.util.Set, but with custom constraint on field 将 Java.Util.Set 加载到 Spring ConfigurationProperties - Loading Java.Util.Set in to Spring ConfigurationProperties ConcurrentModificationException on java.util.Set上的for循环 - ConcurrentModificationException on for loop on java.util.Set java.util.Set无法通过ParameterizedType检查 - java.util.Set is is failing ParameterizedType check 多对多关系:org.hibernate.MappingException:无法确定以下类型:java.util.Set - many to many relationship : org.hibernate.MappingException: Could not determine type for: java.util.Set 在Scala中将java.util.Set转换为java.util.List - Converting a java.util.Set to java.util.List in Scala
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM