1
resposta

Eu tive o mesmo erro do primeiro tópico get id, fiz a solução citada, tive outro erro

Tive esse erro

Exception in thread "main" javax.persistence.RollbackException: Error while committing the transaction at org.hibernate.internal.ExceptionConverterImpl.convertCommitException(ExceptionConverterImpl.java:81) at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:104) at org.example.dao.DAO.adicionar(DAO.kt:20) at org.example.TesteBancoKt.main(TesteBanco.kt:20) at org.example.TesteBancoKt.main(TesteBanco.kt) Caused by: java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : org.example.entity.GamerEntity.plano -> org.example.entity.PlanoEntity


O meu código está idêntico ao da professora(acredito eu), pois como estou seguindo a aula não faz sentido fazer diferente.

fun Plano.toEntity(): PlanoEntity {

return if (this is PlanoAssinatura) {
    if (this == null) {
        throw IllegalArgumentException("PlanoEntity não pode ser nulo")
    }
    PlanoAssinaturaEntity(
        this.tipo, this.mensalidade,
        this.jogosIncluidos,
        this.percentualDescontoReputacao,
        this.id)
} else {
    PlanoAvulsoEntity(this.tipo, this.id)
}

}

class GamersDAO( manager: EntityManager ): DAO<Gamer, GamerEntity>( manager = manager, GamerEntity::class.java ) {

override fun toEntity(gamer: Gamer): GamerEntity = GamerEntity(
        nome = gamer.nome,
        email = gamer.email,
        dataNascimento = gamer.dataNascimento,
        usuario = gamer.usuario,
        plano = gamer.plano.toEntity()
)

override fun toModel(entity: GamerEntity): Gamer = Gamer(
        nome = entity.nome,
        email = entity.email,
        dataNascimento = entity.dataNascimento?:"Data de Nascimento",
        usuario = entity.usuario?: "Sem usuário",
        id = entity.id
    ).apply { plano = entity.plano.toModel() }

// val query = manager.createQuery("FROM GamerEntity", GamerEntity::class.java) // // fun getGamers(): List { // return query.resultList.map { // Gamer( // nome = it.nome, // email = it.email, // dataNascimento = it.dataNascimento?:"sem data de nascimento", // usuario = it.usuario?:"sem usuário") // } // } // // fun adicionarGamer(gamer:Gamer) { // val entity = GamerEntity( // nome = gamer.nome, // email = gamer.email, // dataNascimento = // gamer.dataNascimento, // usuario = gamer.usuario // ) // manager.transaction.begin() // manager.persist(entity) // manager.transaction.commit() // manager.close() // // }

}

class PlanosDAO( manager: EntityManager ) : DAO<Plano, PlanoEntity>( manager = manager, PlanoEntity::class.java ) { override fun toEntity(plano: Plano): PlanoEntity { return plano.toEntity() }

override fun toModel(entity: PlanoEntity): Plano {
    return entity.toModel()
}

}


fun main(){ val gamer = Gamer("Monica", "monica@email.com", "15/03/1995", "moni")

val manager = Banco.getEntityManager()
val gamerDAO = GamersDAO(manager)
val planosDAO = PlanosDAO(manager)

gamer.plano = planosDAO.recuperarPeloId(5)

gamerDAO.adicionar(gamer)

val listaGamersBanco = gamerDAO.getLista()
println(listaGamersBanco)

manager.close()

}

1 resposta

Amigo, bom dia.

Consegues mandar o código completo num repositório do GitHub? É bem melhor pra testar;