2014-02-21 37 views
0

我正在使用休眠标准api来获取txn和txn_products表的数据。 下面是映射。休眠标准没有填充关联

类事务处理:

@Entity 
@DynamicUpdate(value=true) 
@Table(name="txn") 
public class Txn 
{ 
@OneToMany(fetch=FetchType.LAZY , mappedBy = "transaction" , cascade = CascadeType.ALL) 
    Set<TxnProduct> txnProducts = null; 

    @Id 
    @Column(name="id" , nullable=false) 
    private String id; 

...... 
} 

类TxnProduct:

@Entity 
@DynamicUpdate(true) 
@Table(name="txn_product") 
public class TxnProduct 
{ 

@Id 
    @Column(name="id" , nullable=false) 
    private String id; 

    @ManyToOne(fetch=FetchType.LAZY) 
    @JoinColumn(name="txn_id") 
    private Txn transaction ; 
..... 
} 

业务逻辑:

Session sx = .......... ; 
     Criteria Q = sx.createCriteria(Txn.class, "txn"); 
       Q.createAlias("txn.txnProducts", "txnProducts" , JoinType.INNER_JOIN); 

       List<Txn> L = (List<Txn>) Q.list(); 


       logger.info(L) ; 

       for(Txn T : L) 
       { 
        logger.info(T); 
        logger.info(T.getTxnProducts()); 
       } 

     sx.close(); 

在执行业务逻辑List L回报Txn对象在数据库中的每个TxnProduct,但什么我期望从休眠c riteria将为txn表中的每一行返回Txn对象,并在其中设置Set<TxnProduct>

我试过Q.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);但它没有帮助。 谢谢。

回答

1

Q.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);应该可以在大多数情况下工作,除非我们在母公司如setFirstResultsetMaxResult上使用分页。 JoinTypeFetchMode可能有问题。

Set包装q.list()将是更好的选择,不管任何获取模式。类似于 Set<Txn> resutls = new HashSet<Txn>(L));或者如果您想再次返回列表,请使用列表将套餐换行 List<Txn> results = new ArrayList<Txn>(new HashSet<Txn>(L));