简体   繁体   中英

Joined inheritance strategy

I m using inheritance type equal to joined in my spring project .I had a class called Identifier which had 4 types called ( direct , indirect , urgent , info ) . While inserting one identifier with one type like direct or else it is OK .But i want to insert multiple types with the same identifier Id which it is not permitted because the id is a primary key can not be duplicated . How can I achieve this propose ???

Maybe another inheritance strategy could achieve the requested goal.

@Table(name = "identifier")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "TYPE_IDEN", discriminatorType = DiscriminatorType.STRING, length = 2)

Actually , I'm blocked if there is anyway to do that .

You modeled your problem with an inheritance solution. You have one super class with four children. Ask yourself: is possible for any subclass in java to be of more than one type at the same time? Considering what is written in the question, it is what you're trying to do, and it's impossible.

If you want your class to hold more than one set of characteristics, you'll have to reconsider the way you're modeling your problem.

If you need an "Identifier" to hold any number of "types", maybe you should use composition, so instead of an "Identifier" being a "direct" or "indirect", it will contain a "direct" and/or "indirect".

This is only a suggestion, there are multiple solutions and I am not aware of all your needs, but keep in mind that your current modeling won't allow you to have the behavior you described.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM