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

How to Implement the Assigned Identity Generator? #757

Open
hell0w0orld opened this issue Nov 29, 2024 · 0 comments
Open

How to Implement the Assigned Identity Generator? #757

hell0w0orld opened this issue Nov 29, 2024 · 0 comments

Comments

@hell0w0orld
Copy link

The version I am using is Hypersistence-Utils-hibernate-63: 3.8.3

I want to implement the Assigned Identity Generator. I have browsed through your previous blog about its implementation, but the current version is no longer applicable. I am trying to implement the function, but I am not sure if it is correct. I hope to receive your reply. Thank you very much.

Here is the code for the previous blog implementation, blog address: https://vladmihalcea.com/how-to-combine-the-hibernate-assigned-generator-with-a-sequence-or-an-identity-column/

public class AssignedIdentityGenerator
    extends IdentityGenerator {
 
    @Override
    public Serializable generate(SessionImplementor session,
        Object obj) {
        if(obj instanceof Identifiable) {
            Identifiable identifiable = (Identifiable) obj;
            Serializable id = identifiable.getId();
            if(id != null) {
                return id;
            }
        }
        return super.generate(session, obj);
    }
}

Here is a partial code I have implemented:

public class AssignedIdentityGenerator extends Assigned {

    @Override
    public Object generate(SharedSessionContractImplementor s, Object obj) {
        if (obj instanceof SysBase t && t.getId() == null) {
            return IdentifierGeneratorHelper.getGeneratedIdentity(xxx);
        }

        return super.generate(s, obj);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant