使用postgresql的Hibernate Mapping问题

时间:2016-11-25 09:43:42

标签: java postgresql hibernate

我是hibernate的新手。我试图映射以下实体,但获得hibernate映射异常。将非常感谢帮助。 记住,Iam使用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;
    }
}

如下所示获得例外

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

2 个答案:

答案 0 :(得分:2)

如果您没有保存属性oAuth2RefreshToken的任何值,请添加注释@Transient以忽略与数据库的任何映射。

如果您有一些地图,请将其添加到您的问题

答案 1 :(得分:0)

在您的OAuth2RefreshToken (OAuth2AccessToken)类中将接口DefaultOAuth2RefreshToken (DefaultOAuth2AccessToken)更改为EntityDefaultOAuth2RefreshToken (DefaultOAuth2AccessToken)是类,但不是接口。 Postgres BD将其另存为BYTEA

最后,我邀请您阅读此Hibernate链接,以便了解可以在其中使用该实体模型的POJO模型。 休眠https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#entity-pojo