【问题标题】:Hibernate not inserting to mysql: showing insert statementsHibernate 不插入到 mysql:显示插入语句
【发布时间】:2013-11-14 06:28:32
【问题描述】:

我正在使用 spring 和 hibernate 做一个项目。 Hibernate 显示“insert sql”语句,但表是空的。我在stackoverflow中检查了类似的问题。但我找不到任何与我的问题类似的问题。

//这些是我的实体类

//歌曲类

    @Entity
    @Table(name="Song", catalog="myFavMusic")
    public class Song implements Serializable {

        @Id
        @GeneratedValue(strategy = IDENTITY)
        private Integer id;

        public Song(){

        }
        public Song(String title, Album album, Singer singer, Integer rating) {
            super();
            Title = title;
            this.album = album;
             this.singer = singer;
             this.rating = rating;
        }

        private String Title;

        @ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
        @JoinColumn(name = "ALBUM_ID", nullable = false)
        private Album album;

        @ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
        @JoinColumn(name = "SINGER_ID", nullable = false)
        private Singer singer;

        private Integer rating;
    }

// 专辑

    @Entity
    public class Album {    
        public Album(String title, String type, Integer releasedYear) {
            super();
            this.title = title;
            this.type = type;
            this.releasedYear = releasedYear;
        }

        @Id
        @GeneratedValue
        @Column(name = "ALBUM_ID", unique = true, nullable = false, length = 20)
        private Integer id;

        @Column(name = "TITLE", unique = true, nullable = false)
        private String title;

        @Column(name = "TYPE", unique = true, nullable = false)
        private String type;

        @Column(name = "RELEASED_YEAR", unique = true, nullable = false)
        private Integer releasedYear;
    }

// 歌手班

    @Entity
    public class Singer {

        public Singer(String singerName, Date dob) {
           super();
           this.singerName = singerName;
           this.dob = dob;
        }

        @Id
        @GeneratedValue
        @Column(name = "SINGER_ID", unique = true, nullable = false, length = 20)
        private Integer id;
        private String singerName;
        private Date dob;
   }

// 这是我的 DAO 接口

   public interface MusicDao {
           public void addSong(Song song);
       public List<Song> listAllSongsBySpec(SongSpec spec);
   }

// 这是我的 DAO 实现

    public class MusicDaoImpl implements MusicDao {

        @Autowired
        private SessionFactory sessionFactory;

        public void addSong(Song song) {
            sessionFactory.getCurrentSession().save(song);
        }
    }

//弹簧配置

    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource"><ref bean="dataSource" /></property>
    <property name="hibernateProperties">
           <props>
                   <prop  key="hibernate.dialect">  org.hibernate.dialect.MySQLDialect
                    </prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
    <property name="packagesToScan" value="com.myprojects.myfavmusic.domain" />
</bean>
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <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.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>file:src/test/resources/config/database-unitFav.properties
        </value>
    </property>
</bean>
<bean id="musicDao" class="com.myprojects.myfavmusic.dao.impl.MusicDaoImpl"
    autowire="byName">
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

// 我的单元测试来测试这个

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"file:src/test/ApplicationContext- unitFav.xml"})
    public class MusicDaoImplTest extends TestCase {

        @Autowired
        private MusicDao musicDao;
        @Test
        @Transactional
        public void testAddSong() {
            Album album = new Album("album1","movie",2009);
            Singer singer = new Singer("singer 1",new Date());
            Song song = new Song("song 1",album,singer,0);
            musicDao.addSong(song);
            assertTrue(true);
       }

}

这对我来说似乎是一个奇怪的问题。 Hibernate 不会抱怨任何错误。但是当我检查数据库时没有记录。我认为这可能是冲洗模式的问题。但我相信默认的冲洗模式是自动的。无论如何,我也尝试指定冲洗模式。但问题仍然存在。请帮我解决这个问题?

提前致谢, 阿伦

【问题讨论】:

  • 您可能遗漏了一些指示 hibernate 提交结果的说明,可能吗?检查您是否需要发出 sessionFactory.getCurrentSession().commit() 或类似的东西。
  • 我这样做了,但是没有用
  • 将@Transactional 转移到你的DAO。
  • 它工作了 Alan Hay,它是如何解决问题的 :)
  • 因为您的 Test 类不是 Spring 托管 bean,因此 @Transactional 声明无效。

标签: mysql spring hibernate junit transactions


【解决方案1】:

实际上这是我所期望的行为(测试结束后什么都没有)。

当您在测试结束时用@Transactional 标记测试方法时,对该方法所做的所有更改都将自动回滚。 (如参考指南中的here 所述。

当您将 @Transactional 移动到您的 dao 而不是测试用例时,测试不再是事务性的,数据将保留。我不会将此作为最佳实践,因为对于初学者来说,应该是事务性的不是您的 dao,而是您的服务层。除此之外,您不希望来自一项测试的数据干扰另一项测试。

您确实应该在测试方法中测试数据是否存在(而不是使用 sql 浏览器或类似的东西。)

@ContextConfiguration(locations={"file:src/test/ApplicationContext- unitFav.xml"})
public class MusicDaoImplTest extends AbstractTransactionalJUnit4SpringContextTests {

    @Autowired
    private MusicDao musicDao;

    @Autowired
    private SessionFactory sf;

    @Test
    public void testAddSong() {
        Album album = new Album("album1","movie",2009);
        Singer singer = new Singer("singer 1",new Date());
        Song song = new Song("song 1",album,singer,0);
        musicDao.addSong(song);
        sf.getCurrentSession().flush(); // Similate a flush at the end of a transaction
        int count = countRowsInTable("song"); // Data should be in the table
        assertEquals(1, count);
   }

类似的东西。

最后请注意,您的测试用例也有轻微缺陷,您混合了 JUnit3 和 JUnit4 类型。您正在扩展 TestCase,即 JUnit3,而您在要测试的方法上使用 @Test 注释。这是等待发生的麻烦(奇怪的测试结果、执行等)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多