Is it possible to have ressources in other project (Spring Boot)? - mysql

I want to work with MySQL 10 and Point for Geolocations.
My property for the dialect is :
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
On my Entity I use at the Moment:
#Column(name="location", columnDefinition = "POINT")
private org.springframework.data.geo.Point location;
The error message is:
com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Cannot get geometry object from data you send to the GEOMETRY field
I try to save it with a JpaRepository => save()
Is it possible with JPA?

spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.mysql.MySQL56InnoDBSpatialDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.h2geodb.GeoDBDialect
try with both versions of dialect not sure which one will suit your needs

Related

How to insert/update a row in MySQL for JSON column using Java SprinBoot CrudRepository

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.
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.

JPA how to set column type to be BLOB for ElementCollection table

I'm using Spring Boot with Hibernate JPA with the following code in my entity class,
#ElementCollection
private Map<String, String> userFiles= new HashMap<>();
Where a collection table user_files is generated. However the default column type is VARCHAR(255) and sometimes user data can be longer than that. My question is how can I define this table to use BLOB or TEXT for column? I tried to throw in #Lob annotation directly there but does not work.
Have you tried the following?
#Column(columnDefinition="BLOB NOT NULL")
#MapKeyColumn(columnDefinition="BLOB NOT NULL")
See MapKeyColumn and Lob.

Grails: Joda LocalDate as unique key in a MySQL database

I have a domain class with a Joda LocalDate property. This property must be unique.
It works using an H2, but using a MySQL db I have this error on app boot:
[SchemaExport.create(l.386)]Unsuccessful: create table [...]
[SchemaExport.create(l.387)]BLOB/TEXT column 'mydate' used in key specification without a key length
If a remove unique constraint, it works also with MySQL.
Is it a bug or my misunderstanding?
I'm using Grails 2.2.5.
Here domain fragment:
class MyClass {
LocalDate mydate
static constraints = {
mydate(nullable:false, unique:true)
}
}
DataSource config fragment:
dataSource {
dbCreate = "create-drop"
driverClassName = "com.mysql.jdbc.Driver"
dialect = org.hibernate.dialect.MySQL5InnoDBDialect
username = "xxx"
password = "xxx"
dbNamer = "myapp"
url = "jdbc:mysql://localhost:3306/${dbNamer}?autoreconnect=true"
logSql = true
}
wild guess, I think that date is reserved word in mySQL. rename the field and try again
UPDATE:
ok, I see.
the problem is, that GORM/Hibernate is not aware of joda LocalDate class, so it's trying to create a BLOB column for it:
[SchemaExport.create(l.387)]BLOB/TEXT column 'mydate'`.
To fix it, you can either use a custom hibernate type (have no idea how), or convert the property into a plane java's Date class by saving, and back to LocalDate after loading

Grails Scaffold - Generated MySQL Column Type for JodaTime DateTime field

I am new to Grails and am using Grails 2.1 with a MySQL 5.5 backend to build a sample project to learn.
I installed JodaTime 1.4 Plug-in and then ran grails install-joda-time-templates
However, when I declared a Domain Class field to be of type org.joda.time.DateTime, I got an error when attempting to save a new entry.
In order to isolate the problem, I created a simple Domain Class:
import org.joda.time.DateTime
class Project
{
String name
DateTime startDate
static constraints = {
name(blank: false, maxSize: 50)
startDate(validator: {return (it > new DateTime())})
}
}
The controller just sets scaffold to use the Domain Class.
My DataSource.groovy specifies dbCreate = "create-drop", as I am letting the tables get created by Grails.
Here is the error I get when I try to save:
Class:com.mysql.jdbc.MysqlDataTruncation
Message:Data truncation: Data too long for column 'start_date' at row 1
When I look at project.start_date column in the MySQL database that Grails created, the type is TINYBLOB.
My thought is that TINYBLOB may not be sufficient to store the data for a JodaTime DateTime field.
Does anyone know how I can make Grails create an appropriate type?
Thank you very much.
In your Config.groovy:
grails.gorm.default.mapping = {
"user-type" type: PersistentDateTime, class: DateTime
"user-type" type: PersistentLocalDate, class: LocalDate
}
And your mapping closure:
static mapping = {
startDate type: PersistentDateTime
}
Take a look at this post for more info, see if it helps.
What I did to make it work (Grails 2.1):
1) add to buildConfig:
compile "org.jadira.usertype:usertype.jodatime:1.9"
2) refresh the dependencies
3) run this command to add the user-type supported:
grails install-joda-time-gorm-mappings
4) finally in the domain class:
import org.jadira.usertype.dateandtime.joda.*
static mapping = {
column_name type: PersistentDateTime
}
Documentation was found here: persistence

Fluent Nhibernate and Sql Server private setter error

I'm trying to complete some first step with fluent nhibernate with sql server express, to add this features in my project.
But got some errors. As I'm newbie to nhibernate, couldn't solve a problem for some time.
I have an entity with
Id { get; private set; }
accessors. And this entity is mapped to table with identity {1,1} column in sql server.
But during creating Session factory, I get an error:
The following types may not be used as proxies:Entity: method set_Id should be 'public/protected virtual' or 'protected internal virtual'
I understand that private setter is used to encapsulate setting of this property, but why do I get this error then?
PS: example on site of fluent for nhibernate is using sqllite db and everything is just fine.
This is an issue that has caused grief for many NH/FNH users, including me.
Until recently, NHibernate has allowed private setters with proxy objects. But starting with NH 3.2, private setters are no longer allowed - they must be "'public/protected virtual' or 'protected internal virtual'", as the error message says.
This is a breaking change for a lot of the FNH/NH sample code that's out there, and is especially confusing to newbies.
Declare the property as public virtual int Id { get; protected set; }. The proxy object generated by NHibernate will then be able to set the property.
Another solution can be to use a backing field:
private int id;
public int Id
{
get { return id; }
}
And then use mapping:
Map(x => x.Id).Access.CamelCaseField();