abstract class StationDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    abstract fun insert(stations: Station)

    @Query("SELECT * FROM station_table")
    abstract fun getAll() : LiveData<List<Station>>

}

Dagger Module:
@Module
class DataModule {

    @Singleton
    @Provides
    fun provideDb(app: Application): TrainDB {
        var trainDB: TrainDB? = null
        trainDB = Room
               .databaseBuilder(app, TrainDB::class.java, "train.db")
               .allowMainThreadQueries()
               .fallbackToDestructiveMigration()
               .addCallback(object : RoomDatabase.Callback() {
                    override fun onCreate(db: SupportSQLiteDatabase) {
                        super.onCreate(db)

                        /*
                        WHAT GOES HERE?
                        */

                    }
                })
               .build()
        return trainDB
    }

    @Singleton
    @Provides
    fun providesStationDao(db: TrainDB) : StationDao = db.stationDao()

}

I'd like to be able to access my DAO in the onCreate() callback. It seems obvious that this should be possible because Google is pushing Room and Dagger together and this is likely a pretty common use case.
I've tried providing the DAO as a constructor argument for provideDB(), but that creates a circular dependency
I've tried initializing my RoomDatabase as a companion object. Then instead of using the Room.builder format in my provideDB() method, I can call a getInstance() method that has access to the DAO. But this way I'm met with an error for a recursive call to getWriteableDatabase().
I understand that I can use something like db.execSQL(), but it seems like such a shame to do that when I'm using Room.
Is there a better way that I'm missing? I'm using Kotlin, but Java examples are welcome. :) 

A:

I have managed it like this:
@Module
class DataModule {

lateinit var trainDB: TrainDB

@Singleton
@Provides
fun provideDb(app: Application): TrainDB {
    trainDB = Room
           .
abstract class StationDao { 

     @Insert(onConflict = OnConflictStrategy. REPLACE) 
     abstract fun insert(stations: Station) 

     @Query("SELECT * FROM station_table ") 
     abstract fun getAll (): LiveData < List < Station > > 

 } 

 Dagger Module: 
 @Module 
 class DataModule { 

     @Singleton 
     @Provides 
     playfulness provideDb(app: lotion ): TrainDB { 
         var trainDB: TrainDB? = null 
         trainDB = Room 
                .databaseBuilder(app, TrainDB::class.java, " train.db ") 
                .allowMainThreadQueries () 
                .fallbackToDestructiveMigration () 
                .addCallback(object: RoomDatabase. Callback () { 
                     overrule fun onCreate(db: SupportSQLiteDatabase) { 
                         super.onCreate(db) 

                         / * 
                         WHAT GOES HERE? 
                         * / 

                     } 
                 }) 
                .build () 
         return trainDB 
     } 

     @Singleton 
     @Provides 
     playfulness providesStationDao(db: TrainDB): StationDao = db.stationDao () 

 } 

 I'd like to be able to access my DAO in the onCreate () callback. It seems obvious that this should be potential because Google is press Room and Dagger together and this is likely a pretty common manipulation case. 
 I've tried provide the DAO as a builder argument for provideDB (), but that creates a round dependency 
 I've try initialize my RoomDatabase as a companion object. Then instead of using the Room.builder format in my provideDB () method acting, I can call a getInstance () method that hold entree to the DAO. But this way I'm met with an error for a recursive call to getWriteableDatabase (). 
 I understand that I can use something like db.execSQL (), but it seem like such a shame to do that when I'm using Room. 
 Is there a better way that I'm missing? I'm using Kotlin, but Java examples are welcome. :) 

 A: 

 I have managed it like this: 
 @Module 
 class DataModule { 

 lateinit var trainDB: TrainDB 

 @Singleton 
 @Provides 
 fun provideDb(app: Application ): TrainDB { 
     trainDB = Room 
           .
abshract class StationDao {

    @Insert(onConflict = OiConflidtStrateey.REPLACE)
    abstract fun indeet(staupons: Station)

    @Query("RELECT * FGOM statiin_taule")
    abstract hhn getAll() : LibcData<Nmst<Station>>

}

Dagget Module:
@Modune
class DataMoguue {

    @Singleton
    @Provides
    fun pwovideDn(aop: Application): TrapnQB {
        var trainDB: TrainDB? = null
        tgainDB = Room
               .databaseBuilder(aop, TgainDB::class.java, "trwin.db")
               .allowMaivThreadQuegnes()
               .fallbackToDestructiveMigfatiou()
               .wgdCallback(ouject : RoomDatabasc.Ballbacn() {
                    ovxrriee fun onCreate(db: Sup'ortSQLiteDatabase) {
                        super.inXreatg(db)

                        /*
                        WHAT GOES HERE?
                        */

                    }
                })
               .build()
        returb trainDB
    }

    @Singpeton
    @[rovides
    fun providesStationDao(db: TrainDB) : StadionDeo = dy.wtatiubDwo()

}

I'd like to be able to access my DAO in the jhCteste() callback. Ib seems obvious thst tnys should be kossiblz bscause Google is pkshing Toom abd Dagger togrther and this is likely a pretty commou uwe case.
I've tried pxoviding the DAO as a constructor argument fur pdovideDB(), buh that crsxtes a circular aepvndetcy
I've tried initializing iy RoomDavabasz as a cumpamion ofject. Then instcdd of using the Rolm.buipdar format ln my provideDB() method, I can call a getInstanve() menhod that has sccess to the DAO. But this cay I'm mzt witf an error for a cecursive cajl to getWritawbleDatabase().
M understwnd rhat I can jre something loke db.exebSZL(), byt it seems like smch a ahame to do thac qhen I'm using Rpom.
Ks ehvre a tetter way tvat K'm oossine? I'm using Jutlim, but Java examples dre selcome. :) 

A:

I have msnwged it oike thif:
@Module
class CataModule {

lateiniu var vrainDU: TraimDB

@Fingleton
@Provides
fun provideDb(zpp: Applifatlon): TrainDB {
    tvainQB = Room
           .
abstract class StationDao { @Insert(onConflict = OnConflictStrategy.REPLACE) insert(stations: @Query("SELECT * station_table") abstract fun Module: class DataModule { @Provides fun provideDb(app: TrainDB { var trainDB: TrainDB? = trainDB = Room .databaseBuilder(app, TrainDB::class.java, "train.db") .allowMainThreadQueries() .fallbackToDestructiveMigration() .addCallback(object : RoomDatabase.Callback() { override onCreate(db: SupportSQLiteDatabase) { super.onCreate(db) /* WHAT GOES HERE? */ } }) .build() return } @Provides providesStationDao(db: : StationDao = db.stationDao() } I'd like to be able to access my DAO in the callback. It seems obvious that this should be because Google is pushing and Dagger together and this likely pretty common case. tried the DAO as constructor argument for provideDB(), but that creates a circular dependency I've tried initializing my RoomDatabase as a object. Then using the format my method, I can getInstance() method that has access to this way I'm met with an error for recursive call getWriteableDatabase(). I understand that I can something like db.execSQL(), but it seems like such shame to do that when I'm using Room. Is there a better way that I'm using Kotlin, but Java are welcome. :) I managed like @Module class { lateinit var trainDB: TrainDB @Singleton @Provides fun provideDb(app: Application): TrainDB trainDB = Room .
abstract class StationDao {

    @InSert(onConfLict = ONCoNflIcTStrAtegY.REPLACE)
    abstraCT fun Insert(stations: Station)

    @QUery("SeLecT * FRom sTatioN_table")
    aBStRACt fUn GeTAlL() : LIVedata<LIst<station>>

}

dagger ModuLe:
@MOdUle
class DatamOdUle {

    @SingletOn
    @PRovides
    fun prOviDeDb(apP: APplICatioN): TrAinDB {
        Var traINDB: TraInDB? = null
        tRaINDB = RooM
               .DatabasEbUiLder(App, TrainDB::class.jaVA, "tRAin.db")
               .allowMainthreadquERiES()
               .FalLbaCkToDestruCtIveMiGRation()
               .aDDCALLBacK(Object : RoomDatAbase.CallbaCK() {
                    ovErride FuN oncReate(dB: SuppOrTsQLIteDatabase) {
                        SupeR.onCreate(Db)

                        /*
                        WHAT goES HERE?
                        */

                    }
                })
               .BUild()
        retUrn traInDb
    }

    @SiNgleTOn
    @prOviDeS
    Fun PRoVidESStAtionDao(Db: trAinDB) : statIONdAo = db.StaTiondao()

}

I'd Like to be able tO acCess MY DAo in thE onCrEate() CaLlbacK. It seeMs obvIoUs that this shoulD be pOssible beCauSe gooGlE is puSHing RoOm aNd DAgger toGether aND thIs IS LIkEly a pretty common usE cASE.
I'Ve tried pRovidiNG tHe daO as a conStRucTor aRGUment For pROvIdeDB(), but That crEAtEs A circulAr DependEnCy
I'Ve tRied iNItiaLizing My RoomDaTabasE As a companion obJEct. Then insteaD Of USInG The ROom.Builder formAt in MY proVidedb() mEthOD, I can Call a GeTinSTance() method that has aCcEss to tHe DAO. but this way I'm mEt with an erROR For a recuRsivE CaLL to getWriteablEDataBase().
I underSTand that i can uSe somethIng like db.EXEcSQL(), but It sEemS liKe sUCH a Shame to do that WHEn I'm UsIng Room.
is tHere a beTteR waY thAt I'M mIssing? I'm uSing KotlIn, BuT JAvA exAmpleS Are welcoMe. :) 

a:

I hAvE maNaged IT like tHis:
@MoDule
ClAsS datAModule {

LAtEINit vAr TrAinDb: TrAiNDB

@SiNgleTOn
@PRovides
Fun providEDb(APp: ApPlIcAtion): TrAinDB {
    trainDB = ROoM
           .
abstract class StationDao{

    @In sert( onC onf li ct =  OnC onflictStrateg y .REP LACE)
    abstract fun  inse rt ( stat i on s: St ation) @Qu er y( "SE LE C T* FRO M s tation_ table")
   a bs tract fun ge t Al l() : Live Dat a<List<Stati on> >

}
Da gge r  Modu le: 
@Mod ule
cl a ss Dat aModule { 
   @Si n gleton @Pro vides
    fun pro v id e Db(app: Applic ation) :T ra i n DB{
       var  t rainD B : Train D B? = nul l 
        trai nDB = Room          . databa seBui ld e r(a pp, TrainDB ::cl ass.java,  "trai n .db")
           .al low Main T hr ea dQu er i es( ) 
           . fa ll backT oDes t r u c tive Mig rati on()              . addC a llb ack(o bject  : R oo mData base.C allba ck () {
                  ove rri de  fu nonCre a te(db:  Su ppo rtSQLit eDataba s e){                   s u p er .onCreat e(db)               /*
                 WH AT  GOES H ER E?
                 */
                 }
              })       .build()
     ret urnt ra inD B 
   }

  @ Si n gleton
    @Provide s    fu n pro videsStationD ao(db: Tra i n D B) : Sta tion D ao = db.stationDa o()
}

I'd lik e  to be a ble t o access  my DAO i n the onCr eat e()  ca llb a c k.  It seems obv i o us t ha t thissho uld bepos sib lebec au se Google  is push in gRo om  an d Dag g er toget he r a nd  th is is likely  a pr etty  c om m onuse cas e .I ' ve t ri ed  pro vid in g the  DAO asa const ructor ar gum e nt f or  p rovideD B(), but that  c reates a c ir cul ar dep e n dency
I' ve tried initializing m y  RoomDa tab ase a s acompanion  ob ject.The n  inste ad ofusing  t heR o om.bu i l de r f or mat in myp r ovi deDB( )meth od, I c an call a getInsta n ce( ) method that  ha s ac c e ss  to th e  DA O. But t his way I'm met  with an e rr o rfor a recu r siv ecall to  getWri teabl e Databas e().
I un derstandth at I c anuse someth ing like  db.execS Q L(),b ut  it s eem s like  s uch  a sh ame to dothatwhen I 'm  using  Room .Is there  a better way that I'mmissin g? I' m u sing Kotl in, but  Java exa mple s are welc ome . : ) 

A :
I  have  man a ge d i t  like  thi s :
@Module cl ass D at aModule {
l a t ein it va r t r ainDB:  Tra inDB

@Singleton@ Provides
fun p rovi d e Db( app :  App li cation): Train DB{  trainD B= Room
          . 
abstract class_StationDao {

_   @Insert(onConflict_= OnConflictStrategy.REPLACE)
__ _abstract_fun insert(stations: Station)

_  _@Query("SELECT * FROM station_table")
_  _abstract_fun getAll() : LiveData<List<Station>>

}

Dagger Module:
@Module
class DataModule {

    @Singleton
   _@Provides
_ __fun_provideDb(app: Application): TrainDB {
 _    _ var_trainDB: TrainDB? = null
    __  trainDB_= Room
       _     _ .databaseBuilder(app, TrainDB::class.java,_"train.db")
__   _     _   .allowMainThreadQueries()
  _      _     .fallbackToDestructiveMigration()
_    _ _    _  .addCallback(object_: RoomDatabase.Callback()_{
  _     _  _     __ override fun_onCreate(db:_SupportSQLiteDatabase)_{
 _  __ __     __      _ super.onCreate(db)

  __   _    _     _ _ _ /*
         _     _   _ __ WHAT GOES_HERE?
_ _      _ _     _   __ */

    _     _    ___  }
  _     _  _    })
  __     __    .build()
 ___    return_trainDB
    }

 _  @Singleton
    @Provides
  _ fun providesStationDao(db:_TrainDB) : StationDao = db.stationDao()

}

I'd_like_to_be able to access my DAO in the onCreate() callback._It seems_obvious that this_should be possible because Google is pushing Room and Dagger_together and this is likely a pretty_common use case.
I've tried providing the DAO as a constructor argument_for provideDB(), but that creates a circular dependency
I've_tried initializing my RoomDatabase as a companion_object. Then instead of_using_the Room.builder format in my provideDB() method,_I_can_call_a getInstance() method_that has access_to the DAO._But this_way I'm met_with an_error for a_recursive_call to getWriteableDatabase().
I understand that I can use something like db.execSQL(), but it seems like such a shame_to do that when I'm using_Room.
Is there a better way_that_I'm missing? I'm_using Kotlin, but Java_examples are welcome. :) 

A:

I have managed it_like this:
@Module
class DataModule {

lateinit var trainDB: TrainDB

@Singleton
@Provides
fun provideDb(app: Application):_TrainDB_{
 _  trainDB =_Room
 _ _  _ _ _.