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

  • JPA & Hibernate, object not persisting to mysql datbase

    Hi,


    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 jdbc.properties & 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.

    Code:
    @Service
    public class AnimalsServiceImpl implements AnimalsService {
    	
    	private static final Logger logger = LoggerFactory.getLogger(AnimalsServiceImpl.class);
    
    	@Autowired
    	private AnimalsDao animalsDao;
    
    	@Override
    	@Transactional
    	public void saveAnimal(Animal animal) {
    		
    		logger.info("Saving "+animal.getName()+ " to database.");
    
    		animalsDao.saveAnimal(animal);
    		
    		
    	}
    This is my DAO
    Code:
    @Repository
    public class AnimalsDaoImpl implements AnimalsDao {
    	
    	private static final Logger logger = LoggerFactory.getLogger(AnimalsDaoImpl.class);
    	 
    	@PersistenceContext
    	protected EntityManager entityManager;
    
    	@Override
    	public void saveAnimal(Animal animal) {
    		
    		logger.info("DAO, saving animal " +animal.getName() +" ID: " +animal.getAnimalId());
    		
    		entityManager.persist(animal);
    				
    	}

    This is the entity object I'm trying to persist

    Code:
    @Entity
    @Table(name="animals")
    public class Animal implements Serializable  {
    	
    	
    	
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 3006749713606005442L;
    
    	@Id
    	@Column(name = "ANIMAL_ID")
    	@GeneratedValue
    	@NotNull
    	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")
    	@Lob
    	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")
    	@Lob
    	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")
    	@Lob
    	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
    Code:
    <context:property-placeholder location="/WEB-INF/jdbc.properties"/>
    
    
    	
    	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
      		<property name="dataSource" ref="dataSource" />
    	</bean>
    
        <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>
        
        <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
        </bean>
    
         
        <tx:annotation-driven transaction-manager="transactionManager" />
       
    
        
        <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
            <property name="dataSource" ref="dataSource"/>
        </bean>    
        
    <!-- 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"/>
        </bean>

    this is my persistance.xml

    Code:
    	<persistence-unit name="persistenceUnit">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
                <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
                <property name="hibernate.hbm2ddl.auto" 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"/>
            </properties>
        </persistence-unit>
    Last edited by ggunning; Mar 11th, 2013, 09:24 AM.

  • #2
    Hi,

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

    Comment


    • #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

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

      Code:
       <tx:annotation-driven/>
      and I added

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

      Code:
       <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.

      Comment

      Working...
      X