0

This is the scenario

@Transient
private Map<String, String> choices = null;

@Column(name = "choices", nullable = false)
public String getChoices() {
    return gson.toJson(choices);
}

And while inserting record it says

java.sql.SQLException: Field 'choices' doesn't have a default value

My DEBUG SQL

 DEBUG SQL:109 - 
    insert 
    into
        question
        (description, id) 
    values
        (?, ?)

Here Choices field is ignored.

The Database details :

CREATE TABLE `question` (<br/>
  `id` varchar(50) COLLATE utf8_bin NOT NULL,<br/>
  `description` varchar(1000) COLLATE utf8_bin NOT NULL,<br/>
  `choices` text COLLATE utf8_bin NOT NULL,<br/>
  `answers` varchar(100) COLLATE utf8_bin NOT NULL<br/>
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;<br/>
<br/>
ALTER TABLE `question`<br/>
  ADD PRIMARY KEY (`id`);<br/>

Can someone please help me why such fields are ignored ? Thank You.

[UPDATES] The Entity Class :

@Entity
@Table(name = "question")
public class Question {

@Id
private String id = null;

@Column(name = "description", nullable = false)
private String description = null;


@Transient
@Column(name = "choices", nullable = false)
private Map<String, String> choices = null;

public String getChoices() {
    return gson.toJson(choices);
}


@Transient
private Set<String> answers = null;

@Transient
private Gson gson = new GsonBuilder().create();

public Question() {
    this.id = UUID.randomUUID().toString();
}
...
}//End of class
Aryasindhu Sahu
  • 103
  • 1
  • 13

2 Answers2

1

In the create table you have marked the choices column not null. choices text COLLATE utf8_bin NOT NULL, hence the exception.

When you are persisting the Question entity class you do not provide the values for choices so they are not included in the resultant sql. Either provide the value for choices or drop the not null constraint on the column in database.


Annotating an attribute javax.persistence.Transient marks it as non persistent in JPA, and wont be included in the resultant SQL.

Rohit
  • 2,132
  • 1
  • 15
  • 24
  • Hi Rohit, thanks for the reply. I got your comments. But the problem here is I want the column to be Not Null and here the insert query is not taking the values of choices field. I am setting values for choices but due to some configuration issues JPA is not accepting this field. Please help me in that.. – Aryasindhu Sahu Jan 12 '16 at 13:31
  • I have a setter with map as argument . – Aryasindhu Sahu Jan 12 '16 at 13:45
  • I don't want to use the variable to provide value for my table's field. Rather I used a getter method which converts the variable's value into a particular type and provides it to JPA. – Aryasindhu Sahu Jan 12 '16 at 15:10
0

You could simply use the JPA annotation @MapKey (note that the JPA annotation is different from the Hibernate one, the Hibernate @MapKey maps a database column holding the map key, while the JPA's annotation maps the property to be used as the map's key).Use mapping as:

@javax.persistence.MapKey(name = "choices ")
private Map<String, String> choices = new HashMap<String, String>(); 

public String getChoices() {
    return gson.toJson(choices);
}
anand mishra
  • 900
  • 1
  • 11
  • 30