0
votes

My User model

@Entity
@Table(name="users")
data class User(
        @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
        val id: Long = -1,
        @Column(unique=true)
        val username: String) : Serializable

Two records in the database

database

The query is fine.

[2018-02-23T14:32:07.066+0100] [Payara 4.1] [FINE] [] [org.eclipse.persistence.session./file:/Users/youri/Downloads/payara41/glassfish/domains/domain1/applications/Kwetter-1.0-SNAPSHOT/WEB-INF/classes/_kwetter.sql] [tid: _ThreadID=28 _ThreadName=http-thread-pool::http-listener-1(4)] [timeMillis: 1519392727066] [levelValue: 500] [[ SELECT ID, USERNAME FROM users]]

But it outputs two empty objects, instead of two User objects

[{},{}]

Abstract Dao where I use the Entity Manager

@Stateless
abstract class Abstract<T : Serializable> {
    @PersistenceContext
    private lateinit var entityManager: EntityManager

    abstract fun getEntityClass(): Class<T>

    open fun find(id: Long): T {
        return entityManager.find(getEntityClass(), id)
    }

    // this returns the weird two empty objects
    open fun all(): List<T> {
        val builder = entityManager.criteriaBuilder
        val c = builder.createQuery(getEntityClass())
        c.from(getEntityClass())
        val query = entityManager.createQuery(c)

        return query.resultList
    }
}
1
What type are those two objects?Salem
It should print two serialized User objects, but it prints two empty objects...yooouuri
Yes, but string representations are not the most reliable debugging method. What is the class of those elements? i.e. what is the result of item::class? They don't seem to be of type User.Salem
@Moira User::classyooouuri
You defined the attributes as val but they must be mutable. Use var instead of valSimon Martinelli

1 Answers

2
votes

You should use var instead of val since Kotlin won't make a setter for val fields. JPA needs to have mutable properties (which means getters & setters).

TLDR: use var instead of val