Отображение JPA и Hibernate с MySQL: поле 'description' не имеет значения по умолчанию - PullRequest
1 голос
/ 03 декабря 2011

У меня есть следующее исключение, когда я пытаюсь вставить значения в базу данных.Это мой класс постоянства:

<code><pre> 
@SuppressWarnings("serial")
@Entity
@Table(name = "consumer", catalog = "oauth")
@NamedQueries({
    @NamedQuery(name = Consumer.QUERY_BY_CONSUMER_KEY, query = "SELECT i FROM " + Consumer.TABLENAME + " i WHERE i.consumerKey = :" + Consumer.Q_PARAM_CONSUMER_KEY)})
public class Consumer implements java.io.Serializable {

    public static final String TABLENAME = "Consumer";
    public static final String QUERY_BY_CONSUMER_KEY = "ConsumerFindByConsumerKey";
    public static final String Q_PARAM_CONSUMER_KEY = "consumer_key";
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Integer id;
    @ManyToOne
    @JoinColumn(name = "user_id", nullable = false)
    private Idpuser idpuser;
    @Column(name = "description", nullable = false, length = 500)
    private String description;
    @Column(name = "consumer_key", nullable = false, length = 100)
    private String consumerKey;
    @Column(name = "consumer_secret", nullable = false, length = 100)
    private String consumerSecret;
    @Column(name = "callbackurl", nullable = false, length = 400)
    private String callbackURL;
    @Column(name = "request_token", length = 500)
    private String requestToken;
    @Column(name = "token_secret", length = 500)
    private String tokenSecret;
    @Column(name = "access_token", length = 500)
    private String accessToken;
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "acces_token_valid_from", length = 19)
    private Date accesTokenValidFrom;
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "access_token_valid_to", length = 19)
    private Date accessTokenValidTo;
    @Column(name = "access_token_num_usage_count")
    private Integer accessTokenNumUsageCount;
    @Column(name = "access_token_max_usage_count")
    private Integer accessTokenMaxUsageCount;
    @Column(name = "authorized", nullable = false)
    private Boolean authorized;
    @ManyToMany(fetch= FetchType.EAGER)
    @JoinTable(name = "consumer_role_relation",
    joinColumns = { 
      @JoinColumn(name = "consumer_id")
    }, 
    inverseJoinColumns = {
      @JoinColumn(name = "role_id")
    }
    )
    private Set<Role> roles = new HashSet<Role>(0);

    @Column(name = "facebookId", unique = true)
    private BigInteger facebookId;


    protected Consumer() {
    }

    public Consumer(Idpuser idpuser, String describtion, String consumerKey, String consumerSecret, String callbackURL,
            String requestToken, String tokenSecret, String accessToken, boolean authorized) {
        this.idpuser = idpuser;
        this.description = describtion;
        this.consumerKey = consumerKey;
        this.consumerSecret = consumerSecret;
        this.callbackURL = callbackURL;
        this.requestToken = requestToken;
        this.tokenSecret = tokenSecret;
        this.accessToken = accessToken;
        this.authorized = authorized;
    }

    public Consumer(Idpuser idpuser, String describtion, String consumerKey, String consumerSecret, String callbackURL,
            String requestToken, String tokenSecret, String accessToken, boolean authorized,
            Date accesTokenValidFrom, Date accessTokenValidTo, int accessTokenMaxUsageCount) {
        this.idpuser = idpuser;
        this.description = describtion;
        this.consumerKey = consumerKey;
        this.consumerSecret = consumerSecret;
        this.callbackURL = callbackURL;
        this.requestToken = requestToken;
        this.tokenSecret = tokenSecret;
        this.accessToken = accessToken;
        this.authorized = authorized;
        this.accesTokenValidFrom = accesTokenValidFrom;
        this.accessTokenValidTo = accessTokenValidTo;
        this.accessTokenMaxUsageCount = accessTokenMaxUsageCount;
    }

    public Consumer(Idpuser idpuser, String describtion, String consumerKey, String consumerSecret, String callbackURL,
            String requestToken, String tokenSecret, String accessToken, boolean authorized,
            Date accesTokenValidFrom, Date accessTokenValidTo, int accessTokenMaxUsageCount, Set<Role> roles) {
        this.idpuser = idpuser;
        this.description = describtion;
        this.consumerKey = consumerKey;
        this.consumerSecret = consumerSecret;
        this.callbackURL = callbackURL;
        this.requestToken = requestToken;
        this.tokenSecret = tokenSecret;
        this.accessToken = accessToken;
        this.authorized = authorized;
        this.accesTokenValidFrom = accesTokenValidFrom;
        this.accessTokenValidTo = accessTokenValidTo;
        this.accessTokenMaxUsageCount = accessTokenMaxUsageCount;
        this.roles = roles;
    }

    public Consumer(Idpuser idpuser, String describtion, String consumerKey, String consumerSecret, String callbackURL,
            String requestToken, String tokenSecret, String accessToken, boolean authorized,
            Date accesTokenValidFrom, Date accessTokenValidTo) {
        this.idpuser = idpuser;
        this.description = describtion;
        this.consumerKey = consumerKey;
        this.consumerSecret = consumerSecret;
        this.callbackURL = callbackURL;
        this.requestToken = requestToken;
        this.tokenSecret = tokenSecret;
        this.accessToken = accessToken;
        this.authorized = authorized;
        this.accesTokenValidFrom = accesTokenValidFrom;
    }


    public Consumer(Idpuser idpuser, String describtion, String consumerKey, String consumerSecret, String callbackURL,
            String requestToken, String tokenSecret, String accessToken, boolean authorized,
            Date accesTokenValidFrom, Date accessTokenValidTo, Set<Role> roles) {
        this.idpuser = idpuser;
        this.description = describtion;
        this.consumerKey = consumerKey;
        this.consumerSecret = consumerSecret;
        this.callbackURL = callbackURL;
        this.requestToken = requestToken;
        this.tokenSecret = tokenSecret;
        this.accessToken = accessToken;
        this.authorized = authorized;
        this.accesTokenValidFrom = accesTokenValidFrom;
        this.roles = roles;
    }


    public Integer getId() {
        return this.id;
    }

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

    public Idpuser getIdpuser() {
        return this.idpuser;
    }

    public void setIdpuser(Idpuser idpuser) {
        this.idpuser = idpuser;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getConsumerKey() {
        return this.consumerKey;
    }

    public void setConsumerKey(String consumerKey) {
        this.consumerKey = consumerKey;
    }

    public String getConsumerSecret() {
        return this.consumerSecret;
    }

    public void setConsumerSecret(String consumerSecret) {
        this.consumerSecret = consumerSecret;
    }

    public String getCallbackURL() {
        return callbackURL;
    }

    public void setCallbackURL(String callbackURL) {
        this.callbackURL = callbackURL;
    }

    public Date getAccesTokenValidFrom() {
        return accesTokenValidFrom;
    }

    public void setAccesTokenValidFrom(Date accesTokenValidFrom) {
        this.accesTokenValidFrom = accesTokenValidFrom;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public Integer getAccessTokenMaxUsageCount() {
        return accessTokenMaxUsageCount;
    }

    public void setAccessTokenMaxUsageCount(Integer accessTokenMaxUsageCount) {
        this.accessTokenMaxUsageCount = accessTokenMaxUsageCount;
    }

    public Integer getAccessTokenNumUsageCount() {
        return accessTokenNumUsageCount;
    }

    public void setAccessTokenNumUsageCount(Integer accessTokenNumUsageCount) {
        this.accessTokenNumUsageCount = accessTokenNumUsageCount;
    }

    public Date getAccessTokenValidTo() {
        return accessTokenValidTo;
    }

    public void setAccessTokenValidTo(Date accessTokenValidTo) {
        this.accessTokenValidTo = accessTokenValidTo;
    }

    public Boolean getAuthorized() {
        return authorized;
    }

    public void setAuthorized(Boolean authorized) {
        this.authorized = authorized;
    }

    public String getRequestToken() {
        return requestToken;
    }

    public void setRequestToken(String requestToken) {
        this.requestToken = requestToken;
    }

    public String getTokenSecret() {
        return tokenSecret;
    }

    public void setTokenSecret(String tokenSecret) {
        this.tokenSecret = tokenSecret;
    }  

    public Set<Role> getRoles(){
        return this.roles;
    }

    public void setRoles(Set<Role> roles){
        this.roles = roles;
    }

    public void addRole(Role r){
        this.roles.add(r);
    }

    public void removeRole(Role r){
        this.roles.remove(r);
    }

    @Override
    public String toString(){
        return this.description;
    }



    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((consumerSecret == null) ? 0 : consumerSecret.hashCode());
        return result;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Consumer other = (Consumer) obj;
        if (consumerSecret == null) {
            if (other.consumerSecret != null)
                return false;
        } else if (!consumerSecret.equals(other.consumerSecret))
            return false;
        return true;
    }



    /* ###### Listener handling ###########################  */
    @Transient
    private CopyOnWriteArrayList<Object> listeners = new CopyOnWriteArrayList<Object>();

    public CopyOnWriteArrayList<Object> getListeners() {
        return listeners;
    }

    public void setListeners(CopyOnWriteArrayList<Object> listeners) {
        this.listeners = listeners;
    }

    public BigInteger getFacebookId() {
        return facebookId;
    }

    public void setFacebookId(BigInteger facebookId) {
        this.facebookId = facebookId;
    }    
}

Исключение:

<code><pre>
Dez 03, 2011 12:58:21 PM org.hibernate.util.JDBCExceptionReporter logExceptions
Warnung: SQL Error: 1364, SQLState: HY000
Dez 03, 2011 12:58:21 PM org.hibernate.util.JDBCExceptionReporter logExceptions
Schwerwiegend: Field 'describtion' doesn't have a default value
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not insert: [org.idp.persistence.mappings.Consumer]
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1214)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1153)
    at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:695)
    at org.idp.persistence.DataManager.saveOrUpdateEntity(DataManager.java:224)
    at org.idp.persistence.DataManager.saveOrUpdate(DataManager.java:142)
    at org.idp.persistence.mappings.Idpuser.addOrUpdateConsumer(Idpuser.java:259)
    at org.idp.playground.ReadTest.main(ReadTest.java:40)

Когда я пытаюсьчитать из базы данных, это работа.Это также работает, когда я пытаюсь вставить данные в другую таблицу (например, Idpuser).Я также попробовал следующие советы решения:

  • установить значение по умолчанию для поля описания в базе данных

1 Ответ

1 голос
/ 03 декабря 2011

Кажется, что в вашей таблице есть столбец "description" (с b), без значения по умолчанию, тогда как у сущности есть поле, сопоставленное столбцу "description" (с p).

Убедитесь, что ваше сопоставление соответствует столбцам таблицы.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...