Spring + hibernate Pb在加载对象与一个集合从数据库



我有这样两个类A和B

@Entity
@Table
public class A implements Serializable {
@Id
@Column
@GeneratedValue ( strategy = GenerationType.IDENTITY )
private Long                idA;
@Column
private String              nomA;
@OneToMany ( mappedBy = "a" )
private Collection < B >    liste;
..........
}

@Entity
@Table
public class B implements Serializable {
@Id
@Column
@GeneratedValue ( strategy = GenerationType.IDENTITY )
private Long    idB;
@Column
private String  nomB;
@ManyToOne
@JoinColumn ( name = "idA" )
private A       a;
...........
}

这是A刀

@Repository
@Transactional
public class AdaoImpl implements Adao {
@PersistenceContext ( type = PersistenceContextType.EXTENDED )
private EntityManager   em;
@Override
public Long addA ( A a ) {
    em.persist ( a );
    return a.getIdA ( );
}
@Override
public A getA ( Long idA ) {
    return em.find ( A.class , idA );
}
}

这是B刀

@Repository
@Transactional
public class BdaoImpl implements Bdao {
@PersistenceContext ( type = PersistenceContextType.EXTENDED )
private EntityManager   em;
@Override
public Long addB ( B b ) {
    em.persist ( b );
    return b.getIdB ( );
}
@Override
public B getB ( Long idB ) {
    // TODO Auto-generated method stub
    return em.find ( B.class , idB );
}
}

现在是A服务

@Service
@Transactional
public class AserviceImpl implements Aservice {
@Autowired
private Adao    dao;
@Override
public Long ajouterA ( A a ) {
    // TODO Auto-generated method stub
    return dao.addA ( a );
}
@Override
public A retournerA ( Long idA ) {
    // TODO Auto-generated method stub
    return dao.getA ( idA );
}
.......
}

B服务

@Service
@Transactional
public class BserviceImpl implements Bservice {
@Autowired
private Bdao    dao;
@Override
public Long ajouterB ( B b ) {
    // TODO Auto-generated method stub
    return dao.addB ( b );
}
@Override
public B retournerB ( Long idb ) {
    // TODO Auto-generated method stub
    return dao.getB ( idb );
}
....
}
当我用下面的代码测试

时:

ApplicationContext ctx = new ClassPathXmlApplicationContext (
                "spring.xml" );
Aservice aservice = ( Aservice ) ctx.getBean ( "aserviceImpl" );
Bservice bservice = ( Bservice ) ctx.getBean ( "bserviceImpl" );
        A a = new A ( "obj A1" );
        B b1 = new B ( "obj B1" );
        b1.setA ( a );
        B b2 = new B ( "obj B2" );
        b2.setA ( a );
        Long Id = aservice.ajouterA ( a );
        bservice.ajouterB ( b1 );
        bservice.ajouterB ( b2 );
        System.out.println ( ">>>>>>>> "
                + aservice.retournerA ( Id ).toString ( ) );

我了 >>>>>>>> (idA = 1,诺玛= obj A1, liste = null]

我想知道为什么我的列表是空的

在保存一个

a.getListe().add(b1);
a.getListe().add(b2);

then call

Long Id = aservice.ajouterA ( a );

这样做的原因是休眠一级缓存,即会话缓存。它只在会话的生存期内缓存所有附加到会话的实体。

所以如果你调用

em.find ( A.class , idA );

则hibernate将返回缓存的A,其中没有B分配给它。

现在你有两个选择:

    使用
  1. em.refresh(a);
    

    这样hibernate就会从数据库中重新加载a。但为此你还需要声明fetchType。正如@HarshPoddar已经指出的,急于与B的关系

  2. 将所有B加到a

    a.getListe().add(b1);
    a.getListe().add(b2);
    

    @ laugh已经发了。

我选2。选择。

最新更新