JPA2.0使用Map (2) 双向一对多,令人湿望的Hibernate

@Entity
public class E {
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Id
	int id;
	String name;
	@OneToMany(mappedBy = "e")
	@MapKeyColumn(name = "kkk")
	Map<String,F> f = new HashMap<String,F>();
}

@Entity
public class F {
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Id
	int id;
	String name;
	@ManyToOne
	E e;
}

public class MapRelation {
	public static void main(String[] args) {
		EntityManagerFactory emf = Persistence
				.createEntityManagerFactory("forcascade");
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();
		
		E e = new E();
        e.name = "i am e!";
        F f1 = new F();
        f1.name = "i am f1!";
        F f2 = new F();
        f2.name = "i am f2!";

        f1.e = e;
        f2.e = e;
        em.persist(f1);
        em.persist(f2);
        e.f.put("f1", f1);
        e.f.put("f2", f2);
        em.persist(e);

		em.getTransaction().commit();
		em.close();
		emf.close();
	}
}

以上代码,JPA2.0双向一对多,在EclipseLink以及OpenJPA下运行没有问题,两个key "f1"与"f2"都存入数据库。但在Hibernate下,key没有存入数据库,失败鸟...
我们想,双向一对多,没有中间表,One将ID以及key存在Many表中,那么,应该可以在Many中设置One的persist cascade,最后提交Many既可以了...
	@ManyToOne(cascade = {PERSIST,REMOVE})
	E e;

在transaction之间的代码是这样的了,
        E e = new E();
        e.name = "i am e!";

        F f1 = new F();
        f1.name = "i am f1!";
        f1.e = e;
        e.f.put("f1", f1);
        em.persist(f1);

        F f2 = new F();
        f2.name = "i am f2!";
        f2.e = e;
        e.f.put("f2", f2);
        em.persist(f2);

高兴的是,EclipseLink与OpenJPA仍然没有让我们失望;杯具地是,Hibernate仍然让我们失望了...

你可能感兴趣的:(java,Hibernate,F#)