My Environment
- mysql 8.0.25
- hibernate-core:5.4.32
- hibernate-spatial:5.4.32
- spring-boot2.5.4
- java 8
What I did
application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/database?serverTimezone=UTC&characterEncoding=UTF-8
username: root
password: password
jpa:
hibernate.ddl-auto: create
generate-ddl: true
database: mysql
properties:
hibernate.dialect: org.hibernate.spatial.dialect.mysql.MySQL56SpatialDialect
logging:
level:
org:
hibernate:
SQL: debug
type: trace
Entity class
import com.example.mypackage.domain.BaseTimeEntity;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.geo.Point;
import javax.persistence.*;
@Getter
@NoArgsConstructor
@Entity
public class Party extends BaseTimeEntity { // BaseTimeEntity adds modifiedAt, createdAt columns
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(columnDefinition = "TEXT")
private String title;
@Column(columnDefinition = "POINT")
private Point coordinate;
@Builder
public Party(Point coordinate, String title, String body) {
this.coordinate = coordinate;
this.title = title;
this.body = body;
}
}
Test
@SpringBootTest
class PartyRepositoryTest {
@Autowired
PartyRepository partyRepository;
@Test
public void register_party() {
// Given
Double x = 127.02558;
Double y = 37.30160;
Point coordinate = new Point(x, y);
partyRepository.save(
Party.builder()
.coordinate(coordinate)
.title("test title")
.build()
);
// When
List<Party> partyList = partyRepository.findAll();
// Then
Party party = partyList.get(0);
assertEquals(x, party.getCoordinate().getX());
assertEquals(y, party.getCoordinate().getY());
}
What I expected
insert row in 'party' table successfully
What actually happened
I got error. Log is as below.
insert into party (created_at, modified_at, body, coordinate, title) values (?, ?, ?, ?, ?)
binding parameter [1] as [TIMESTAMP] - [2021-09-12T14:45:31.018]
binding parameter [2] as [TIMESTAMP] - [2021-09-12T14:45:31.018]
binding parameter [3] as [VARCHAR] - []
binding parameter [4] as [VARBINARY] - [Point [x=127.025580, y=37.301600]]
binding parameter [5] as [VARCHAR] - [test title]
SQL Error: 1416, SQLState: 22001
Data truncation: Cannot get geometry object from data you send to the GEOMETRY field
Question
- Please Let me know what am I doing wrong?
- does hibernate-spatial supports mysql point?