Exposed: Is it possible create an entity class from a table with a composite primary key?

Created on 30 Aug 2016  路  19Comments  路  Source: JetBrains/Exposed

It seems the API forces the IdTable derived objects have got only one key column. Is there any equivalent to the JPA's EmbeddedId and IdClass annotations?

enhancement

Most helpful comment

Any news?

All 19 comments

Hi, @jorginius .
It's not possible at the moment, but I hope we can fix it in a while.
Do you have any experience with EmbeddedId or IdClass approach? Or maybe you can share your vision how it might be implemented to be mostly useful?

Hi, @Tapac .

I commented about the EmbeededId (and Embeddable) annotation because I think it could be the less disruptive approach (one object key, one parameter), but I'm not sure if mimicking JPA is the right way on this point:

@Embeddable
public class UserToRoleKey {
   @Column(nullable = false)
   private String code;
   @Column(nullable = false)
   private String username;
   // ...
}

@Entity
public class UserToRole {
    @EmbeddedId
    private UserToRoleKey key;
    // ...
}

// ...
UserToRoleKey key = new UserToRoleKey();
key.setCode("USER");
key.setUsername("test);
entityManager.find(UserToRole.class, key);

"Raw" Table objects can hold a composite key easily:

internal object UsersToRoles : Table("users_roles") {
    val code = reference("code", Roles.code).primaryKey()
    val username = reference("username", Users.username).primaryKey()
}

Perhaps the ideal API :-) has a EntityClass which works with Table objects (creating EntityId from primaryId fields automatically) or IdTable is deprecated/superseded by the "vanilla" Table, i don't know.

It is my only problem in current project! Please lead me with a workaround at least!

@kamyar1979 , sad, but there is not workaround atm, because entityId is a hardly coupled with a single column instance. We have uncompleted refactoring with this feature support, but it requires a lot of testing before release, so we can't give any ETA :(

Sorry! I have to return to old Hibernate for now!

Need this feature as well :(

Any news?

A very ugly and <8 bit int-specific work around I've been using:

object dgmtypeeffects: IntIdTable(columnName = "typeID\" << 8 | \"effectID") {
    val typeID = integer("typeID").primaryKey()
    val effectID = integer("effectID").primaryKey()
    val isDefault = bool("isDefault")
    val effect = reference("effectID", dgmeffects)
    val type = reference("typeID", invtypes)

    fun idFromPKs(typeID: Int, effectID: Int): Int {
        return typeID shl 8 or effectID
    }

    fun findFromPKs(typeID: Int, effectID: Int): dgmtypeeffect? {
        return dgmtypeeffect.findById(idFromPKs(typeID, effectID))
    }

}

class dgmtypeeffect(id: EntityID<Int>): IntEntity(id) {
    companion object: IntEntityClass<dgmtypeeffect>(dgmtypeeffects)

    var typeID by dgmtypeeffects.typeID
    var effectID by dgmtypeeffects.effectID
    var isDefault by dgmtypeeffects.isDefault
    val effect by dgmeffect referencedOn dgmtypeeffects.effect
    val type by invtype referencedOn dgmtypeeffects.type

}

findByPKs returns the correct object, and accessing these by references in other tables works, I can't say more than that.

The shift could be adjusted, and more values could be added.

seems there is a solution here: https://github.com/JetBrains/Exposed/issues/239

Have there been any updates on this?

@Hc747, not yet.

This issue was created 4 years ago, how is this not fixed yet?

This issue was created 4 years ago, how is this not fixed yet?

You should ask for your money back

@Tapac is there any README about this on-going feature that the community can read and try to contribute?

Bumping because this is ridicolous, I just made an IntIdTable now without using the Id ever.

Bump :)

5 years later we still need it. Really )

Bump :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gcscaglia picture gcscaglia  路  3Comments

barry-m picture barry-m  路  3Comments

raderio picture raderio  路  4Comments

fmgonsalves picture fmgonsalves  路  3Comments

ncobc picture ncobc  路  3Comments