0

I am trying to insert a row in MySQL JSON column using Java SpringBoot CrudRepository. I already skimmed through multiple posts but couldn't get answer for this.

I defined a custom query as:

@Modifying
@Transactional
@Query(value = "insert into cs_test_json(config) values (':data')", nativeQuery = true)
	public void setData(@Param("data") String string);

and MySQL table(cs_test_json) is :

id int
config JSON

In my Controller I wrote

JSONObject data = new JSONObject();
		data.put("name", "kc");
		data.put("age", 28);
		data.put("city", "Abad");

		repository.setData(data.toString());

When I run this application I always get (on calling setData method) com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Invalid JSON text: "Invalid value." at position 0 in value for column 'cs_test_json.config'

Please suggest me what I am missing. I am using MySQL 8

Thank you very much.

1 Answer 1

1

I spotted the ':data'. Can you give a try with :data.? It's a string that you pass and it doesn't need a quotation to further qualify. Spring data takes care of it.

I'm replying from a mobile. Kindly excuse typos and formatting.

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

Comments

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.