Announcement Announcement Module
No announcement yet.
JPA & Hibernate, object not persisting to mysql datbase Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • JPA & Hibernate, object not persisting to mysql datbase


    I am working on a project using spring mvc, jpa, hibernate & a mysql database.

    When I try to persist an object to the database nothing happens, no errors no data in the database. I can run queries and they come back correctly. Any help on this would be greatly appreciated!!

    The really strange thing is that with a few changes to the my & persistanec.xml to point it to an oracle db it works fine.

    I have been stuck on this for a week now and I have read through countless boards & forums all with no joy

    This is my service.

    public class AnimalsServiceImpl implements AnimalsService {
    	private static final Logger logger = LoggerFactory.getLogger(AnimalsServiceImpl.class);
    	private AnimalsDao animalsDao;
    	public void saveAnimal(Animal animal) {
   "Saving "+animal.getName()+ " to database.");
    This is my DAO
    public class AnimalsDaoImpl implements AnimalsDao {
    	private static final Logger logger = LoggerFactory.getLogger(AnimalsDaoImpl.class);
    	protected EntityManager entityManager;
    	public void saveAnimal(Animal animal) {
   "DAO, saving animal " +animal.getName() +" ID: " +animal.getAnimalId());

    This is the entity object I'm trying to persist

    public class Animal implements Serializable  {
    	private static final long serialVersionUID = 3006749713606005442L;
    	@Column(name = "ANIMAL_ID")
    	private Integer animalId;
    	@Column(name = "ANIMALTYPE")
    	private String animalType;
    	@Column(name = "BREED")
    	private String breed;
    	@Column(name = "NAME")
    	private String name;
    	@Column(name = "IMAGE_1")
    	private Blob image1;
    	@Column(name = "IMAGE_1_CONTENT_TYPE")
    	private String image1ContentType;
    	@Column(name = "IMAGE_1_FILE_NAME")
    	private String image1FileName;
    	@Column(name = "IMAGE_1_FILE_SIZE")
    	private String image1FileSize;
    	@Column(name = "IMAGE_2")
    	private Blob image2;
    	@Column(name = "IMAGE_2_CONTENT_TYPE")
    	private String image2ContentType;
    	@Column(name = "IMAGE_2_FILE_NAME")
    	private String image2FileName;
    	@Column(name = "IMAGE_2_FILE_SIZE")
    	private String image2FileSize;
    	@Column(name = "IMAGE_3")
    	private Blob image3;
    	@Column(name = "IMAGE_3_CONTENT_TYPE")
    	private String image3ContentType;
    	@Column(name = "IMAGE_3_FILE_NAME")
    	private String image3FileName;
    	@Column(name = "IMAGE_3_FILE_SIZE")
    	private String image3FileSize;	
    	@Column(name = "ABOUT")
    	private String about;
    	@Column(name = "DATE_IN")
    	private Date dateIn;
    	@Column(name = "DATE_OUT")
    	private Date dateOut;
    	@Column(name = "STATUS")
    	private String status;
    This is my root-context.xml
    <context:property-placeholder location="/WEB-INF/"/>
    	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
      		<property name="dataSource" ref="dataSource" />
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${jdbc.driverClassName}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
        <tx:annotation-driven transaction-manager="transactionManager" />
        <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
            <property name="dataSource" ref="dataSource"/>
    <!-- Configure the multipart resolver -->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!-- one of the properties available; the maximum file size in bytes 1.5mb -->
            <property name="maxUploadSize" value="1572864"/>

    this is my persistance.xml

    	<persistence-unit name="persistenceUnit">
                <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
                <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
                <property name="" value="update"/>
                <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
                <property name="hibernate.show_sql" value="true"/> 
                <property name="hibernate.connection.autocommit" value="true"/>
    Last edited by ggunning; Mar 11th, 2013, 09:24 AM.

  • #2

    I know similar problems to this have been posted thousands of times but no matter what I try nothing seems to resolve it.


    • #3
      Got it working! Finally!

      Here is a summary of the changes I had to make.

      In Animal class (entity object) I changed the id field from a Integer to a long. (I doubt that was anything to do with the fix though!) and removed the implements Serializable.

      In the root context I changed the tx from

      <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>

      and I added

       <context:component-scan base-package="com.lasthope.web"/>
      Then in my servlet context I added

       <context:component-scan base-package="com.lasthope.web.controllers" />
      It looks like it was a clash between the compenet scans.

      Why it worked while pointing to an Oracle database I will never know.