2

春と冬眠を使って予言をしています。「insert sql」ステートメントを表示する休止状態ですが、テーブルは空です。スタックオーバーフローで同様の質問を確認しました。しかし、私の問題に似た質問は見つかりませんでした。

// これらは私のエンティティ クラスです

//ソングクラス

    @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 はエラーを報告しません。しかし、DBを確認したところ、レコードがありません。これは、フラッシュモードの問題である可能性があると思いました。しかし、デフォルトのフラッシングモードは自動だと思います。とにかく、フラッシングモードも指定しようとしました。しかし、それでも問題は存在します。これについて私を助けてください。

前もってありがとう、アルン

4

1 に答える 1

3

実際、これは私が期待する動作です (テストの終了後は何もありません)。

テストの最後に でテスト メソッドをマークすると@Transactional、そのメソッドで行われたすべての変更が自動的にロールバックされます。(リファレンス ガイドで説明されているとおりです。

@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これはトラブル(奇妙なテスト結果、実行など)が起こるのを待っています。

于 2013-11-04T08:27:49.817 に答える