Exposed icon indicating copy to clipboard operation
Exposed copied to clipboard

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

Open jorginius opened this issue 8 years ago • 33 comments

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?

jorginius avatar Aug 30 '16 14:08 jorginius

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?

Tapac avatar Sep 05 '16 11:09 Tapac

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.

jorginius avatar Sep 06 '16 10:09 jorginius

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

kamyar1979 avatar Sep 11 '17 19:09 kamyar1979

@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 :(

Tapac avatar Sep 12 '17 14:09 Tapac

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

kamyar1979 avatar Sep 12 '17 15:09 kamyar1979

Need this feature as well :(

OleksandrSamsonov avatar Oct 23 '17 16:10 OleksandrSamsonov

Any news?

MigiKun avatar May 09 '18 16:05 MigiKun

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.

rnett avatar Jun 12 '18 02:06 rnett

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

jcflorezr avatar Aug 14 '18 21:08 jcflorezr

Have there been any updates on this?

Hc747 avatar Mar 20 '19 01:03 Hc747

@Hc747, not yet.

Tapac avatar Mar 20 '19 06:03 Tapac

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

DRSchlaubi avatar Apr 27 '20 10:04 DRSchlaubi

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

You should ask for your money back

kimble avatar Jun 22 '20 08:06 kimble

I believe that I found a solution: https://stackoverflow.com/questions/57633755/kotlin-exposed-entity-for-table-with-no-identity-column

brunoazevedomendonca avatar Jul 01 '20 23:07 brunoazevedomendonca

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

PedroD avatar Oct 15 '20 12:10 PedroD

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

downloadpizza avatar Jan 04 '21 12:01 downloadpizza

Bump :)

Displee avatar Mar 08 '21 20:03 Displee

5 years later we still need it. Really )

akamuza avatar Mar 31 '21 21:03 akamuza

Bump :)

simonnepomuk avatar Apr 16 '21 06:04 simonnepomuk

bump

azariiva avatar Aug 06 '21 08:08 azariiva

bump! ⏳

PGliw avatar Aug 30 '21 21:08 PGliw

Bump 🆙

Dezzley avatar May 04 '22 11:05 Dezzley

grafik

bump

downloadpizza avatar May 06 '22 09:05 downloadpizza

Bump

romsper avatar Jul 03 '22 01:07 romsper

bump

trebsoj avatar Oct 03 '22 14:10 trebsoj

bump

MoktanP avatar Oct 13 '22 04:10 MoktanP

happy new year

well-balanced avatar Jan 03 '23 08:01 well-balanced

bump

vladovello avatar Sep 25 '23 09:09 vladovello

bump

fengelniederhammer avatar Sep 25 '23 11:09 fengelniederhammer

Merry christmas guys

Nek-12 avatar Dec 25 '23 14:12 Nek-12