我的程序有两个实体:
@Entity
public class User implements Serializable {
@ManyToMany(cascade={CascadeType.ALL},fetch = FetchType.EAGER,
targetEntity = Ip.class,mappedBy = "user")
@Cascade(org.hibernate.annotations.CascadeType.ALL)
@Fetch(value = FetchMode.SUBSELECT)
private Collection<Ip> allowedIp;
...
}
@Entity
public class Ip implements Serializable {
@Column(unique=false,updatable=true,insertable=true,nullable=true,
length=255,scale=0,precision=0)
@Id
private String value;
@ManyToMany(targetEntity = User.class)
private Collection<User> user;
...
}
我尝试使用Spring JPA存储库来坚持新用户,如下所示:
@Transactional (readOnly = false)
@Repository
public interface UserRepository extends JpaRepository<User, String> {
}
List<Ip> allowedIp = new ArrayList<Ip>();
allowedIp.add(ipRepository.findOne("*"));
User user = new User();
user.setAllowedIp(allowedIp);
userRepository.save(user);
问题是Ip(*值)永远不会持久化,尽管我添加了JPA级联注释,还有hibernate级联注释。知道为什么会出现这个问题吗?
答案 0 :(得分:1)
您将@manytomany标记为由IP类(mappedBy注释)管理。您必须将用户添加到IP.user集合以保持关系。
for (Ip ip : allowedIp) ip.getUser().add(user)