mercredi 25 février 2015

hibernate doesn't add new record to database

I would like to add new record to databse using hibernate and entityManager. When I use entityMenager.persist(myObject) console displays "Hibernate: select nextval ('hibernate_sequence')" but I don't see below insert sql.


It is my entity class:



@Entity
@Table(name = "author")
public class Author implements Serializable{

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "author_id")
private Integer author_id;

@Column(name = "author")
private String author;

public Integer getAuthor_id() {
return author_id;
}

public void setAuthor_id(Integer author_id) {
this.author_id = author_id;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}
}


And it is DAO:



@Repository
@Transactional
public class AuthorDAO {

@Autowired
private EntityManager entityManager;

public void addNewAuthor(){
Author newAuthor = new Author();
newAuthor.setAuthor("Dan Brown");
entityManager.persist(newAuthor);
}
}


Configuration is below:



<persistence-unit name="engineerJPA" transaction-type="JPA">
<class>com.engineering.pawel.entity.User</class>
<class>com.engineering.pawel.entity.UserRole</class>
<class>com.engineering.pawel.entity.Author</class>

<jta-data-source>java:jboss/datasources/postgreSQL</jta-data-source>

<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.jdbc.batch_size" value="100" />
<property name="hibernate.id.new_generator_mappings" value="true" />
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
<property name="hibernate.transaction.auto_close_session" value="true" />
<property name="javax.persistence.transactionType " value="jta" />
<property name="hibernate.current_session_context_class" value="jta" />
<property name="hibernate.connection.release_mode" value="auto" />
</properties>
</persistence-unit>
</persistence>


and context:



<context:annotation-config />
<context:component-scan base-package="com.engineering.pawel" />

<!-- Database configuration -->
<tx:annotation-driven />

<jee:jndi-lookup id="entityManagerFactory" jndi-name="java:comp/env/persistence/emf"
expected-type="javax.persistence.EntityManagerFactory" />

<beans:bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<beans:property name="transactionManagerName" value="java:/TransactionManager" />
</beans:bean>

<beans:bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<beans:bean id="entityManager"
class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<beans:property name="entityManagerFactory" ref="entityManagerFactory" />
</beans:bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>


Framework's versions ( maven ):



<spring.version>4.0.0.RELEASE</spring.version>
<spring.security.version>3.2.5.RELEASE</spring.security.version>
<hibernate-version>4.0.1.Final</hibernate-version>


I am using entityMenager to login and is ok.



@Repository
public class UserDAO{

@Autowired
private EntityManager entityManager;

@SuppressWarnings("unchecked")
public List<User> getAllUsers(){
Query query = entityManager.createQuery("from User");
List<User> listUsers = query.getResultList();
return listUsers;
}

@SuppressWarnings("unchecked")
public User findByUserName(String userName){

List<User> users = new ArrayList<User>();

Query query = entityManager.createQuery("from User where nick = :nick").setParameter("nick", userName);
users = query.getResultList();

if(users.size() > 0){
return users.get(0);
}else{
return null;
}
}
}

Aucun commentaire:

Enregistrer un commentaire