EntityManager.find() does not find entity
<p>I'm having a problem with JPA and Hibernate where <code>EntityManager.find()</code> or even <code>EntityManager.createQuery()</code> will not return an entity in my database with the corresponding ID. The former returns <code>null</code> and the latter throws a <code>No entity found for query</code> exception. I know for a fact that the entity with that ID exists in the database because I'm staring at the entry as the code is run.</p>

<p>There are a few steps I take to get to where it fails. First, I add two items to my DayItem table and return them to the client. I change their values on the client and use a POST to send them back to the server. Once received by the server I create a new <code>EntityManager</code> and attempt to delete the entities by ID by getting their class and ID and passing these values to <code>EntityManager.find(Class, ID)</code>. </p>

<p>This finds the entity in the database with its original values, not the changed ones sent over from the client. <code>EntityManager.find(Class, ID)</code> is also exactly where the problem is happening. Once the entity from the database is found, I call <code>EntityManager.remove()</code> on the entity to remove it.</p>

<p>The reason that I don't use <code>EntityManager.merge()</code> directly on the entities right when they are received by the server is because their unique keys were swapped, and this would cause a constraint violation unless I delete them from the database before running <code>EntityManager.merge()</code>.</p>

<p>Finally, after the entities from the database are removed, I add them back with their updated values by calling <code>EntityManager.merge()</code> on them.</p>

<p>Please take a look at my code below, any help with why <code>EntityManager.find()</code> returns null and why the query throws the <code>No entity found for query</code> exception would be a big help.</p>

<p>Here is the unique key on the DayItem table:</p>

<pre><code>ALTER TABLE DAY_ITEM
ADD CONSTRAINT DAY_ITEM_UK
UNIQUE (EMAIL, MOD_ID, FM_ORDER, DAYS_DATE);
</code></pre>

<p>Here is the DayItem table entity:</p>

<pre><code>@Entity
@Table(name = "DAY_ITEM")
public class DayItem implements GtEntity, Serializable{
    private Long id;
    private String email;
    private String name;
    private BigDecimal amount;
    private Integer modId;
    private Integer fmOrder;
    private String modName;
    private Date daysDate;
    private String daysDateString;
    private Long foodId;
    private Long mealId;
    private FoodItem foodItem;
    private MealItem mealItem;
    private ArrayList&lt;FoodItem&gt; foodItems;
    private ArrayList&lt;MealItem&gt; mealItems;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Basic
    @Column(name = "EMAIL", nullable = false, length = 50)
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Basic
    @Column(name = "NAME", nullable = true, length = 100)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Basic
    @Column(name = "AMOUNT", nullable = true)
    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    @Basic
    @Column(name = "MOD_ID", nullable = false)
    public Integer getModId() {
        return modId;
    }

    public void setModId(Integer modId) {
        this.modId = modId;
    }

    @Basic
    @Column(name = "FM_ORDER", nullable = false)
    public Integer getFmOrder() {
        return fmOrder;
    }

    public void setFmOrder(Integer fmOrder) {
        this.fmOrder = fmOrder;
    }

    @Basic
    @Column(name = "MOD_NAME", nullable = true, length = 50)
    public String getModName() {
        return modName;
    }

    public void setModName(String modName) {
        this.modName = modName;
    }

    @Basic
    @Column(name = "DAYS_DATE", nullable = false)
    public Date getDaysDate() {
        return daysDate;
    }

    public void setDaysDate(Date daysDate) {
        this.daysDate = daysDate;
    }

    @Transient
    public String getDaysDateString() {
        return daysDateString;
    }

    public void setDaysDateString(String daysDateString) {
        this.daysDateString = daysDateString;
    }

    @Basic
    @Column(name = "FOOD_ID", nullable = true)
    public Long getFoodId() {
        return foodId;
    }

    public void setFoodId(Long foodId) {
        this.foodId = foodId;
    }

    @Basic
    @Column(name = "MEAL_ID", nullable = true)
    public Long getMealId() {
        return mealId;
    }

    public void setMealId(Long mealId) {
        this.mealId = mealId;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "FOOD_ID", referencedColumnName = "ID",
            updatable = false, insertable = false)
    public FoodItem getFoodItem() {
        return foodItem;
    }

    public void setFoodItem(FoodItem foodItem) {
        this.foodItem = foodItem;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "MEAL_ID", referencedColumnName = "ID",
            updatable = false, insertable = false)
    public MealItem getMealItem() {
        return mealItem;
    }

    public void setMealItem(MealItem mealItem) {
        this.mealItem = mealItem;
    }

    @Transient
    public ArrayList&lt;FoodItem&gt; getFoodItems() {
        return foodItems;
    }

    public void setFoodItems(ArrayList&lt;FoodItem&gt; foodItems) {
        this.foodItems = foodItems;
    }

    @Transient
    public ArrayList&lt;MealItem&gt; getMealItems() {
        return mealItems;
    }

    public void setMealItems(ArrayList&lt;MealItem&gt; mealItems) {
        this.mealItems = mealItems;
    }

    public void makeDaysDate() throws ParseException{
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        this.daysDate = new Date(formatter.parse(this.daysDateString).getTime());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof DayItem)) return false;

        DayItem dayItem = (DayItem) o;

        if (!getEmail().equals(dayItem.getEmail())) return false;
        if (!getModId().equals(dayItem.getModId())) return false;
        if (!getFmOrder().equals(dayItem.getFmOrder())) return false;
        return getDaysDate().equals(dayItem.getDaysDate());

    }

    @Override
    public int hashCode() {
        int result = getEmail().hashCode();
        result = 31 * result + getModId().hashCode();
        result = 31 * result + getFmOrder().hashCode();
        result = 31 * result + getDaysDate().hashCode();
        return result;
    }
}
</code></pre>

<p>Here's the code that adds a DayItem:</p>

<pre><code>@Override
public List&lt;DayItem&gt; saveDayItem(DayItem item) throws Exception {
    List&lt;DayItem&gt; results = null;
    EntityManager manager = createEntityManager();
    EntityTransaction tx = manager.getTransaction();
    try {
        tx.begin();
        DayItem managedItem = manager.merge(item);
        manager.flush();
        manager.clear();
        tx.commit();
        results = findUserDay(managedItem.getDaysDate(), managedItem.getEmail(), manager);
    } catch (RuntimeException e) {
        tx.rollback();
        throw e;
    } finally {
        manager.close();
    }
    return results;
}
</code></pre>

<p>Here's the code that deletes the DayItems:</p>

<pre><code>public &lt;T extends GtEntity&gt; void batchDelete(List&lt;T&gt; entities) throws Exception {
    if (!entities.isEmpty()) {
        EntityManager manager = createEntityManager();
        EntityTransaction tx = manager.getTransaction();
        try {
            tx.begin();
            for (int i = 0; i &lt; entities.size(); i++) {
                if (i % this.jdbcBatchSize == 0) {
                    manager.flush();
                    manager.clear();
                }

                T del = entities.get(i);
                T managed = manager.find((Class&lt;T&gt;)del.getClass(), del.getId());
                //The line above is where the problem happens

                manager.remove(managed);
            }
            manager.flush();
            manager.clear();
            tx.commit();
        } catch (RuntimeException e) {
            tx.rollback();
            throw e;
        }
    }
}
</code></pre>