Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jorginius opened this issue Aug 30, 2016 · 32 comments
Assignees

Comments

@jorginius
Copy link

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?

@Tapac
Copy link
Contributor

Tapac commented Sep 5, 2016

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?

@jorginius
Copy link
Author

jorginius commented Sep 6, 2016

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.

@kamyar1979
Copy link

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

@Tapac
Copy link
Contributor

Tapac commented Sep 12, 2017

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

@kamyar1979
Copy link

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

@OleksandrSamsonov
Copy link

Need this feature as well :(

@MigiKun
Copy link

MigiKun commented May 9, 2018

Any news?

@rnett
Copy link

rnett commented Jun 12, 2018

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.

@jcflorezr
Copy link

seems there is a solution here: #239

@Hc747
Copy link

Hc747 commented Mar 20, 2019

Have there been any updates on this?

@Tapac
Copy link
Contributor

Tapac commented Mar 20, 2019

@Hc747, not yet.

@DRSchlaubi
Copy link
Contributor

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

@kimble
Copy link

kimble commented Jun 22, 2020

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

You should ask for your money back

@brunoazevedomendonca
Copy link

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

@PedroD
Copy link

PedroD commented Oct 15, 2020

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

@downloadpizza
Copy link

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

@Displee
Copy link

Displee commented Mar 8, 2021

Bump :)

@akamuza
Copy link

akamuza commented Mar 31, 2021

5 years later we still need it. Really )

@simonnepomuk
Copy link

Bump :)

@azariiva
Copy link

azariiva commented Aug 6, 2021

bump

@PGliw
Copy link

PGliw commented Aug 30, 2021

bump! ⏳

@Dezzley
Copy link

Dezzley commented May 4, 2022

Bump 🆙

@downloadpizza
Copy link

grafik

bump

@romsper
Copy link

romsper commented Jul 3, 2022

Bump

@trebsoj
Copy link

trebsoj commented Oct 3, 2022

bump

1 similar comment
@MoktanP
Copy link

MoktanP commented Oct 13, 2022

bump

@well-balanced
Copy link

happy new year

@vladovello
Copy link

bump

1 similar comment
@fengelniederhammer
Copy link

bump

@bog-walk bog-walk self-assigned this Dec 8, 2023
@Nek-12
Copy link

Nek-12 commented Dec 25, 2023

Merry christmas guys

@ilyasKerbal
Copy link

@Nek-12 Merry Christmas, any updates?

@Nek-12
Copy link

Nek-12 commented Mar 2, 2024

@Nek-12 Merry Christmas, any updates?

Yeah, lots of updates: I finished school, got into university, finished university, started my masters, found a job and left that job. What a great 8 years!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests