0

我有两个实体,即 People 和 Story。现在我发布我的故事并检查我的故事,但偶尔会返回不同的故事列表。我的意思是, usr.stories.hashCode() 在来自完全相同的用户的不同请求中是不一样的。数据库表总是正确的,所以我很困惑。我查看了 Hibernate 参考以获得更好的细节,但它根本没有帮助。

型号类:

@MappedSuperclass
public class People{
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Integer id;
    @OrderBy(value = "id DESC")
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "people")
    public List<Story> stories = new LinkedList<Story>();

    public People(){
    }
}

@MappedSuperclass
public class Story{
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Integer id;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "people_id")
    public People people;
    @Column(name = "story", nullable = false)
    public String story;

    public Story(String story){
        this.story = story;
    }
}

休息类:

@Path("/story")
public class StoryRS {
    @POST
    public StreamingOutput create(final @Context HttpServletRequest req, final String story) throws IOException{
        return new StreamingOutput() {
           @Override
           public void write(OutputStream os) {
               HttpSession hs = req.getSession(false);
               if (hs != null && story != null && story.trim().length() > 0) {
                   Integer uid = (Integer) hs.getAttribute("uid");
                   People usr = uid != null ? PeopleDAO.findById(uid) : null;
                   if (usr != null) {
                      Story obj = new Story(story);
                      EntityManagerHelper.beginTransaction();
                      StoryDAO.save(obj);
                      EntityManagerHelper.commit();
                      os.write(obj.id);
                   }
               }
               os.close();
           }
        };
    }

    @GET
    public StreamingOutput create(final @Context HttpServletRequest req) throws IOException{
        return new StreamingOutput() {
           @Override
           public void write(OutputStream os) {
               HttpSession hs = req.getSession(false);
               if (hs != null && story != null && story.trim().length() > 0) {
                   Integer uid = (Integer) hs.getAttribute("uid");
                   People usr = uid != null ? PeopleDAO.findById(uid) : null;
                   if (usr != null && usr.stories != null && usr.stories.size()>0) {
                      os.write(usr.stories...);
                   }
               }
               os.close();
           }
        };
    }
}

人道:

public class PeopleDAO{
@Override
public void save(People entity) {
    try {
        getEntityManager().persist(entity);
    } catch (RuntimeException re) {
        EntityManagerHelper.log("save failed", Level.SEVERE, re);
        throw re;
    }
}

@Override
public void delete(People entity) {
    try {
        entity = getEntityManager().getReference(People.class, entity.getId());
        getEntityManager().remove(entity);
    } catch (RuntimeException re) {
        EntityManagerHelper.log("delete failed", Level.SEVERE, re);
        throw re;
    }
}

@Override
public People update(People entity) {
    try {
        People result = getEntityManager().merge(entity);
        return result;
    } catch (RuntimeException re) {
        EntityManagerHelper.log("update failed", Level.SEVERE, re);
        throw re;
    }
}

@Override
public People findById(Integer id) {
    try {
        People instance = getEntityManager().find(People.class, id);
        return instance;
    } catch (RuntimeException re) {
        EntityManagerHelper.log("find failed", Level.SEVERE, re);
        throw re;
    }
}

故事道:

public class StoryDAO{
@Override
public void save(Story entity) {
    try {
        getEntityManager().persist(entity);
    } catch (RuntimeException re) {
        EntityManagerHelper.log("save failed", Level.SEVERE, re);
        throw re;
    }
    entity.People.stories.add(0, entity);
}

@Override
public void delete(Story entity) {
    try {
        entity = getEntityManager().getReference(Story.class, entity.getId());
        getEntityManager().remove(entity);
    } catch (RuntimeException re) {
        EntityManagerHelper.log("delete failed", Level.SEVERE, re);
        throw re;
    }
    entity.People.stories.remove(entity);
}

@Override
public Story update(Story entity) {
    try {
        Story result = getEntityManager().merge(entity);
        return result;
    } catch (RuntimeException re) {
        EntityManagerHelper.log("update failed", Level.SEVERE, re);
        throw re;
    }
}

@Override
public Story findById(Integer id) {
    try {
        Story instance = getEntityManager().find(Story.class, id);
        return instance;
    } catch (RuntimeException re) {
        EntityManagerHelper.log("find failed", Level.SEVERE, re);
        throw re;
    }
}

EntityManagerHelper:

public class EntityManagerHelper {
private static final EntityManagerFactory emf;
private static final ThreadLocal<EntityManager> threadLocal;
private static final Logger logger;

static {
    emf = Persistence.createEntityManagerFactory("db");
    threadLocal = new ThreadLocal<EntityManager>();
    logger = Logger.getLogger("db");
    logger.setLevel(Level.ALL);
}

public static EntityManager getEntityManager() {
    EntityManager manager = threadLocal.get();
    if (manager == null || !manager.isOpen()) {
        manager = emf.createEntityManager();
        threadLocal.set(manager);
    }
    return manager;
}

public static void closeEntityManager() {
    EntityManager em = threadLocal.get();
    threadLocal.set(null);
    if (em != null)
        em.close();
}

public static void beginTransaction() {
    getEntityManager().getTransaction().begin();
}

public static void commit() {
    getEntityManager().getTransaction().commit();
}

public static void rollback() {
    getEntityManager().getTransaction().rollback();
}

public static Query createQuery(String query) {
    return getEntityManager().createQuery(query);
}

非常感谢您的任何回复

4

1 回答 1

0

这是你的问题

public static EntityManager getEntityManager() {
    EntityManager manager = threadLocal.get();
    if (manager == null || !manager.isOpen()) {
        manager = emf.createEntityManager();
        threadLocal.set(manager);
    }
    return manager;
}

EntityManager 应该是短暂的对象。它们制造成本低,破坏成本低。它还解释了为什么您会得到不一致的结果。EntityManagers 有一个非常激进的内部缓存。

使用 EM 的规则是“每个事务一个 EntityManager”。

每次更改代码以获取新的 EM,您的问题就会消失。

于 2013-05-12T14:32:38.637 回答