多态查询简单: entityManager.createQuery(从玩具玩具中选择玩具"); //返回所有Toy实例,无论是骑自行车还是乘汽车 entityManager.createQuery(从选择自行车"中选择自行车"); //仅返回自行车类型的玩具实例在上述表达式中,您显然可以使用所有其他JPQL构造,例如WHERE,joins等.如果您碰巧使用了JPA2.0,则关于多态性有一些新功能,例如类型检查和类型转换,请参见此处: http://wiki.eclipse.org/EclipseLink/Release/2.1.0/JPAQueryEnhancements How to configure multiple one to many relationship to the same entity?Consider two classes Boy and Toy. They look as shown belowclass Boy { @OneToMany(mappedBy='ownedBy',fetch=FetchType.EAGER, cascade=CascadeType.ALL) private List<Toy> bikes; ... @OneToMany(mappedBy='ownedBy',fetch=FetchType.EAGER, cascade=CascadeType.ALL) private List<Toy> cars; ...class Toy { @ManyToOne private Boy ownedBy; ...I know that this config is wrong. But I dont know what needs to be configured here.What configuration should be made to the @OneToMany and @ManyToOne annotations, both sides?I got the following exception message before I added the 'mappedBy' to the annotationsjava.sql.SQLException: Field 'cars_Id' doesn't have a default value at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2683) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2144) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2444) at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1997)But there was a join table like 'boy_toy' created when there was no 'mappedBy' added to the annotation. After adding it I don't see the table created in the database.UpdateAlso boy_toy table had three columns namely boy_id, cars_id and bikes_id. But ideally it should be like boy_id a not null and others nullable columns. But all the columns generated were not-nullable. I tried to remove the 'mappedBy' from the annotation and manually edit the table structure to make cars_id and bikes_id to nullable. It worked like a wonder.So now, the only remaining question I have isHow can we dictate hibernate to configure a join table with nullable and not-nullable columns? 解决方案 Use inheritance, create a 1-N relationship between BOY and TOY, with inheritance strategy SINGLE_TABLE. To retrieve only Bikes or Cars connected to a Boy, use polymorhic queries.Bonus: the above setup would not work even if bikes and cars were different classes. The reason is, you cannot have multiple 1-N relationships with fetch=FetchType.EAGER. More on this in my blog: MultipleBagFetchException and Java equalityEdit: polymorphic queriesSimple:entityManager.createQuery("SELECT toy FROM Toy toy"); // returns all Toy instances, regardless of being bike or carentityManager.createQuery("SELECT bike FROM Bike bike"); // returns only Bike type instances of toysin the above expressions, you can obviously use all other JPQL constructs, like WHERE, joins, etc.If you happen to use JPA2.0, there are some new features with regards to polymorphism like type checks and type casts, see here: http://wiki.eclipse.org/EclipseLink/Release/2.1.0/JPAQueryEnhancements 这篇关于Hibernate中的多个1:多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-15 16:19