question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Micronaut Data JDBC does not support non-Long Generated ID types

See original GitHub issue

When using Micronaut Data JDBC, using Id types that are not Long will yield an error.

Steps to Reproduce

1. DDL:

create table if not exists example
(
    id uuid DEFAULT uuid_generate_v1() PRIMARY KEY,
    name text not null unique,
    for_date timestamptz not null,
    created_at timestamp default current_timestamp not null,
    notes text
);

2. Entity:

@MappedEntity
public class Example {

	@Id
	@GeneratedValue
	private String id;

	private String name;

	Date forDate;

	@DateCreated
	Date createdAt;

	///... removed constructors getters and setters for brevity
}

3. Repository

@JdbcRepository(dialect = Dialect.POSTGRES)
public interface ExampleRepository extends CrudRepository<Example, String> {
	Example find(String name);
}

4. Query:

Example example = new Example("Foo", new Date());
exampleRepository.save(example);

Expected Behaviour

Data should be saved without any errors

Actual Behaviour

Exception is thrown:

io.micronaut.data.exceptions.DataAccessException: SQL Error executing INSERT: Bad value for type long : 9c401364-5a01-11ea-803a-0242ac320500
        at io.micronaut.data.jdbc.operations.DefaultJdbcRepositoryOperations.lambda$persistOne$9(DefaultJdbcRepositoryOperations.java:660)
        at io.micronaut.transaction.support.AbstractSynchronousTransactionManager.executeWrite(AbstractSynchronousTransactionManager.java:177)
        at io.micronaut.data.jdbc.operations.DefaultJdbcRepositoryOperations.persistOne(DefaultJdbcRepositoryOperations.java:612)
        at io.micronaut.data.jdbc.operations.DefaultJdbcRepositoryOperations.persist(DefaultJdbcRepositoryOperations.java:602)

Environment Information

  • Operating System: MacOS
  • Micronaut Version: 1.3.2
  • JDK Version: java version 11.0.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
graemerochercommented, Feb 28, 2020

To make it more efficient it would probably be better to do something like:

DataType dataType = insert.getIdentity().getDataType();
Object id;
switch(dataType) {
        case LONG:
            id = generatedKeys.getLong(1);
        break;
         /// ... other cases
        default:
            id = generatedKeys.getObject(1, identity.getType())
}

0reactions
graemerochercommented, Mar 3, 2020

Please go ahead

Read more comments on GitHub >

github_iconTop Results From Across the Web

Micronaut Data - GitHub Pages
Since Micronaut Data is a build time tool, it will not work correctly unless ... no methods but defines the entity type and...
Read more >
Access a database with Micronaut Data ... - Micronaut Guides
GenericRepository. A root interface that features no methods but defines the entity type and ID type as generic arguments.
Read more >
Is there a way to export the DDL to a file with Micronaut Data ...
Micronaut Data JDBC comes with just a basic schema generator, you cannot enable the file export, but you can enable the query logging:...
Read more >
Implement Database Queries and Build a Micronaut Application
Create Micronaut Data entities that map to Oracle Database tables ... public class Owner { // The ID of the class uses a...
Read more >
JPA Data Access with Micronaut Data - Piotr's TechBlog
Currently, Micronaut Predator provides runtime support for JPA (Hibernate) and SQL (JDBC). Some other implementations are planned in the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found