【发布时间】:2015-05-29 22:43:53
【问题描述】:
我有两个像这样的 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;
...........
}
这是阿道
@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 ( ) );
我得到了 >>>>>>>> A [idA=1, nomA=obj A1, liste=null]
我想知道为什么我的列表是空的
【问题讨论】:
-
试试
@OneToMany ( mappedBy = "a", fetch=FetchType.EAGER) -
还是一样的结果
标签: java spring hibernate jpa-2.0 spring-data-jpa