0

I am new to hibernate. I am trying to map the following entity but getting hibernate mapping exception. Help will be much appreciated. Remember, Iam using PostgreSQL

package st.malike.auth.server.model;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;


@Entity
public class OAuth2AuthenticationRefreshToken implements Serializable {

//    @Indexed
    @javax.persistence.Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private String id;
    private final String tokenId;
    private final OAuth2RefreshToken oAuth2RefreshToken;
    private final OAuth2Authentication authentication;

    public OAuth2AuthenticationRefreshToken(OAuth2RefreshToken oAuth2RefreshToken, OAuth2Authentication authentication) {
        this.oAuth2RefreshToken = oAuth2RefreshToken;
        this.authentication = authentication;
        this.tokenId = oAuth2RefreshToken.getValue();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTokenId() {
        return tokenId;
    }

    public OAuth2RefreshToken getoAuth2RefreshToken() {
        return oAuth2RefreshToken;
    }

    public OAuth2Authentication getAuthentication() {
        return authentication;
    }
}

And getting exception as below

Caused by: org.hibernate.MappingException: Could not determine type for: org.springframework.security.oauth2.common.OAuth2RefreshToken, 
  at table: oauth2authentication_refresh_token, 
  for columns: [org.hibernate.mapping.Column(o_auth2refresh_token)]     
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:396) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:369) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.mapping.Property.isValid(Property.java:225) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:595) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.mapping.RootClass.validate(RootClass.java:265) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:443) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ~[hibernate-entitymanager-5.0.9.Final.jar:5.0.9.Final]
    ... 162 common frames omitted
4
  • please provide the schema of your table too Commented Nov 25, 2016 at 9:49
  • Do you intend to store the value of all your fields into your database? especially the fields oAuth2RefreshToken and authentication, do you want them to be stored into the db? if not annotate them with @Transient or add the modifier transient to their declaration Commented Nov 25, 2016 at 9:53
  • @Zulfi configuration is spring.datasource.url=jdbc:postgresql://localhost:5432/eCoreAS spring.datasource.username=postgres spring.datasource.password=postgre spring.datasource.driver-class-name=org.postgresql.Driver spring.datasource.maxActive=10 spring.datasource.maxIdle=5 spring.datasource.minIdle=2 spring.datasource.initialSize=5 spring.datasource.removeAbandoned=true spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect spring.jpa.generate-ddl=true spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=create Commented Nov 25, 2016 at 10:05
  • If you do want to store OAuth2AuthenticationRefreshToken, and OAuth2Authentication in your DB, you need to specify the mapping to hibernate (to which other hibernate entity it is linked). Is that what you intend to do ? Commented Nov 25, 2016 at 10:52

2 Answers 2

2

If you are not saving any value for the attribute oAuth2RefreshToken, then add the annotation @Transient to ignore any mapping with the database.

If you have some mapping, please add it to your question

Sign up to request clarification or add additional context in comments.

Comments

0

Change interface OAuth2RefreshToken (OAuth2AccessToken) into DefaultOAuth2RefreshToken (DefaultOAuth2AccessToken) in your Entity class. DefaultOAuth2RefreshToken (DefaultOAuth2AccessToken) are class but not interface. Postgres BD will save it as BYTEA.

Finally, I invite you to read this Hibernate Link in order to know the POJO Model which can use that Entity Model in Hibernate https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#entity-pojo

1 Comment

I suggest you edit your answer to also explain why (your reasoning) OP should make your proposed changes. In this way your answer becomes much more helpful for future readers. Also refer to the contribution guideline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.