2016-02-27 32 views
1

我有User类和BattleReportILogItem类。此类(UserBattleReportILogItem)为@EntityJPA ManyToMany加注为不加载集合中的所有项目

User have 0..NBattleReportILogItem

用户

@Entity 
    @Table(name = DomainConstant.TABLE_USER) 
    public class User implements Serializable { 

     @Id 
     @Column(name = DomainConstant.DOMAIN_USER_ID) 
     @GeneratedValue 
     private Long userId; 

     @ManyToMany(cascade = {CascadeType.ALL}) 
     @JoinTable(name = DomainConstant.VIEW_USER_BATTLE_LOGS, joinColumns = { 
      @JoinColumn(name = DomainConstant.DOMAIN_USER_ID)}, inverseJoinColumns = { 
      @JoinColumn(name = DomainConstant.DOMAIN_BATTLE_REPORT_ID)}) 
     private Set<BattleReportILogItem> setOfBattleLogs = new HashSet<>(); 

....(other stuff, get and set methods...) 

BattleReportILogItem

@Entity 
@Table(name = DomainConstant.TABLE_BATTLE_REPORT) 
public class BattleReportILogItem implements Serializable { 



     @Id 
     @GeneratedValue 
     @Column(name = DomainConstant.DOMAIN_BATTLE_REPORT_ID) 
     private Long BattleReportILogItemId; 

     @ManyToMany(mappedBy = "setOfBattleLogs") 
     private Set<User> setOfBattleLogs = new HashSet<>(); 

     ....(other stuff, get and set methods...) 

的问题是,我加载User程序加载在private Set<BattleReportILogItem> setOfBattleLogs = new HashSet<>();的所有数据。这意味着1 000 000 000项在我的集合setOfBattleLogs。我不想将数据加载到set。对于负载数据我有BattleReportLogItemDaoDAO

有没有解决方案如何NOT LOAD DATA我的set

我希望你能理解我...... :-))

谢谢你的帮忙。

编辑persistence.xml

<?xml version="1.0" encoding="UTF-8"?> 
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> 

    <persistence-unit name="com.donutek" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 

     <properties> 
      <property name="hibernate.hbm2ddl.auto" value="update"/> 
      <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/> 

      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> 

      <property name="hibernate.connection.password" value=""/> 
      <property name="hibernate.connection.username" value="root"/> 
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/db"/> 

      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/> 
      <property name="hibernate.validator.apply_to_ddl" value="true" /> 

      <property name="connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/> 
      <property name="hibernate.c3p0.min_size" value="5"/> 
      <property name="hibernate.c3p0.max_size" value="20"/> 
      <property name="hibernate.c3p0.timeout" value="300"/> 
      <property name="hibernate.c3p0.max_statements" value="50"/> 
      <property name="hibernate.c3p0.idle_test_period" value="300"/> 

     </properties> 
    </persistence-unit> 
</persistence> 

编辑2: 对于我使用的代码加载用户:

@Override 
public User findByEmail(String email) { 
    TypedQuery<User> q = em.createQuery("SELECT u FROM " + User.class.getSimpleName() + " u WHERE u.email = :uemail", User.class); 
    q.setParameter("uemail", email); 
    try { 
     return q.getSingleResult(); 
    } catch (NoResultException e) { 
     return null; 
    } 
} 
+0

您正在使用哪个JPA提供程序? – Bunti

+0

@Bunti嗨,我编辑帖子,看看更新。 – wroumaeg

+0

对于M-N,默认加载是懒惰的,所以只有在您请求时才会加载。或者使用entityGraphs来控制加载。由于您没有发布用于加载对象的代码,因此无法进一步发表评论 –

回答

0

可以使用参数fetchtype懒惰。现在你的策略似乎是渴望的。

+0

'@ManyToMany(cascade = CascadeType.ALL,fetch = FetchType。LAZY)'它是一样的,它加载所有的数据。 – wroumaeg

+0

来自HIbernate docs,'默认情况下,Hibernate使用集合的懒惰选择读取和单值关联的惰性代理读取。这些默认值对大多数应用程序中的大多数关联都有意义。请参见[here](http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch20.html#performance-fetching -lazy) – Bunti

+0

好的,那时我很不好。你能否提供这个获取的dao方法?你只需使用实体管理器? –

相关问题