Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring, Hibernate & JPA: Calling persist on entitymanager does not seem to commit to database
    primarykey
    data
    text
    <p>I'm trying to setup Spring using Hibernate and JPA, but when trying to persist an object, nothing seems to be added to the database.</p> <p>Am using the following:</p> <pre><code>&lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt; &lt;property name="url" value="${jdbc.url}"/&gt; &lt;property name="driverClassName" value="${jdbc.driverClassName}"/&gt; &lt;property name="username" value="${jdbc.username}"/&gt; &lt;property name="password" value="${jdbc.password}"/&gt; &lt;/bean&gt; &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;property name="persistenceUnitName" value="BankingWeb" /&gt; &lt;property name="jpaVendorAdapter"&gt; &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt; &lt;property name="generateDdl" value="true" /&gt; &lt;property name="showSql" value="true" /&gt; &lt;property name="databasePlatform" value="${hibernate.dialect}" /&gt; &lt;/bean&gt; &lt;/property&gt; &lt;/bean&gt; &lt;tx:annotation-driven/&gt; &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt; &lt;property name="entityManagerFactory" ref="entityManagerFactory"/&gt; &lt;/bean&gt; &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt; &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/&gt; &lt;bean name="accountManager" class="ssel.banking.dao.jpa.AccountManager" /&gt; &lt;bean name="userManager" class="ssel.banking.dao.jpa.UserManager" /&gt; </code></pre> <p>And in AccountManager, I'm doing:</p> <pre><code>@Repository public class AccountManager implements IAccountManager { @PersistenceContext private EntityManager em; /* -- 8&lt; -- Query methods omitted -- 8&lt; -- */ public Account storeAccount(Account ac) { ac = em.merge(ac); em.persist(ac); return ac; } } </code></pre> <p>Where ac comes from:</p> <pre><code> Account ac = new Account(); ac.setId(mostRecent.getId()+1); ac.setUser(user); ac.setName(accName); ac.setDate(new Date()); ac.setValue(0); ac = accountManager.storeAccount(ac); return ac; </code></pre> <p>Is there anyone who can point out what I'm doing wrong? The persist call returns without throwing exceptions. If afterwards I do <code>em.contains(ac)</code>, this returns true.</p> <p>In case anyone needed, here's how Account is defined:</p> <pre><code>@SuppressWarnings("serial") @Entity @NamedQueries({ @NamedQuery(name = "Account.AllAccounts", query = "SELECT a FROM Account a"), @NamedQuery(name = "Account.Accounts4User", query = "SELECT a FROM Account a WHERE user=:user"), @NamedQuery(name = "Account.Account4Name", query = "SELECT a FROM Account a WHERE name=:name"), @NamedQuery(name = "Account.MaxId", query = "SELECT MAX(a.id) FROM Account a"), @NamedQuery(name = "Account.Account4Id", query = "SELECT a FROM Account a WHERE id=:id"), }) public class Account extends AbstractNamedDomain { @Temporal(TemporalType.DATE) @Column(name = "xdate") private Date date; private double value; @ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE}) @JoinColumn(name="userid") private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } @OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER) @OrderBy("date") private List&lt;AccountActivity&gt; accountActivity = new ArrayList&lt;AccountActivity&gt;(); public List&lt;AccountActivity&gt; getAccountActivity() { return accountActivity; } public void setAccountActivity(List&lt;AccountActivity&gt; accountActivity) { this.accountActivity = accountActivity; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public double getValue() { return value; } public void setValue(double value) { this.value = value; } public void addAccountActivity(AccountActivity activity) { // Make sure ordering is maintained, JPA only does this on loading int i = 0; while (i &lt; getAccountActivity().size()) { if (getAccountActivity().get(i).getDate().compareTo(activity.getDate()) &lt;= 0) break; i++; } getAccountActivity().add(i, activity); } } @MappedSuperclass public abstract class AbstractNamedDomain extends AbstractDomain { private String name; public AbstractNamedDomain() { } public AbstractNamedDomain(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } @MappedSuperclass public abstract class AbstractDomain implements Serializable { @Id @GeneratedValue private long id = NEW_ID; public static long NEW_ID = -1; public long getId() { return id; } public void setId(long id) { this.id = id; } public boolean isNew() { return id==NEW_ID; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload