2016-02-27 27 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方法?你只需使用實體管理器? –

相關問題