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.

Jpa Mapping quoting exception

See original GitHub issue

I cant seem to get jpa mapping working.

I have an entity like this:

@Entity
data class Ticket(
    @Id
    val ticketId: String = UUID.randomUUID().toString(),
    val jobId: String = UUID.randomUUID().toString(),
    val entityId: String? = "",
    val stepNo: Long = 0,
    val opType: String,
    @Enumerated(EnumType.STRING)
    val status: TicketStatus = TicketStatus.NEW,
    val createdBy: String? = "",
    val resetBy: String? = "",
    val scheduledStartTime: Timestamp = Timestamp.from(Instant.now()),
    val scheduledEndTime: Timestamp? = null,
    val startTime: Timestamp? = null,
    val endTime: Timestamp? = null,
    val retryable: Boolean = true,
    val retryCount: Int = 0,
    val cleanupStep: Int = 0,
    val assignedTo: String? = "",
    val externalRefId: String? = "",
    @field:Column(length = 1000)
    val ticketDetail: String? = "",
    @CreationTimestamp
    val createdTime: Timestamp = Timestamp.from(Instant.now()),
    val updatedBy: String? = "",
    val dryRun: Boolean = false,
    val cancelRequested: Boolean = false,
    val abortCode: String? = null,
    val errorCode: String? = null
)

and when I do

return PostgreSqlUtils.tryGetPGConnection(jdbcTemplate.dataSource?.connection).map {
            logger.info("Using postgres copy for insertion")
            val mapping = JpaMapping(Ticket::class.java)
            val bulkInsert = PgBulkInsert(mapping)
            bulkInsert.saveAll(it, tickets)
            true
        }.orElse(false)

I get the following exception:

String index out of range: 0
java.lang.StringIndexOutOfBoundsException: String index out of range: 0
	at java.lang.String.charAt(String.java:658)
	at de.bytefish.pgbulkinsert.util.PostgreSqlUtils.requiresQuoting(PostgreSqlUtils.java:69)
	at de.bytefish.pgbulkinsert.util.PostgreSqlUtils.quoteIdentifier(PostgreSqlUtils.java:50)
	at de.bytefish.pgbulkinsert.mapping.AbstractMapping.lambda$getCopyCommand$9(AbstractMapping.java:363)
	at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
	at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
	at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
	at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
	at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
	at de.bytefish.pgbulkinsert.mapping.AbstractMapping.getCopyCommand(AbstractMapping.java:364)
	at de.bytefish.pgbulkinsert.PgBulkInsert.saveAll(PgBulkInsert.java:39)
	at de.bytefish.pgbulkinsert.PgBulkInsert.saveAll(PgBulkInsert.java:46)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:17 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
bytefishcommented, Jun 7, 2020

Yes and there is one of the Problems already. I really regret adding a JpaMapping. The Probability is way too high, that the default assumptions (int in java -> int4 in Postgres) fail to hold. This means you would have to pollute your mappings with a million explicit PgDataType annotations… leaving users like you wondering why use it at all then?

Just the same with the DateTime. What should it be mapped to by default? TimestampTz or Timestamp?

You are totally right, most of this can be solved by reading the actual database schema using Hibernate, but I don’t think I can implement it. There are also limits to my skills. This makes me really think: I’ll drop the JpaMapping<> and make a simplified version as a recipe in the README.

Just because I cannot get it easily working as a Drop-In.

1reaction
jeffzochcommented, Jun 7, 2020
column_name data_type character_maximum_length
ticket_id text NULL
abort_code text NULL
assigned_to text NULL
cancel_requested boolean NULL
cleanup_step numeric NULL
created_by text NULL
created_time timestamp without time zone NULL
dry_run boolean NULL
end_time timestamp without time zone NULL
entity_id text NULL
error_code text NULL
external_ref_id text NULL
job_id text NULL
op_type text NULL
reset_by text NULL
retry_count numeric NULL
retryable boolean NULL
scheduled_end_time timestamp without time zone NULL
scheduled_start_time timestamp without time zone NULL
start_time timestamp without time zone NULL
status text NULL
step_no numeric NULL
ticket_detail text NULL
updated_by text NULL

Here is a dump of the schema from pg

Read more comments on GitHub >

github_iconTop Results From Across the Web

Not Mapped to a Single Property Exception in JPA Mapping
I have tried by the following for OneToMany mapping between Style and StyleExp @Entity(name="Style") @Table(name="Style") public class Style ...
Read more >
Guide to JPA with Hibernate - Relationship Mapping
In this article, we'll dive into Relationship Mapping with JPA and Hibernate in Java. The Java Persistence API (JPA) is the persistence ...
Read more >
Chapter 2. Mapping Entities - Red Hat on GitHub
JPA entities are plain POJOs. Actually, they are Hibernate persistent entities. Their mappings are defined through JDK 5.0 annotations instead of hbm.xml ...
Read more >
Defining Unique Constraints in JPA - Baeldung
Learn how to define unique constraints on entity classes in JPA and ... Let's start by creating a domain entity and mapping it...
Read more >
How to escape SQL reserved keywords with JPA and Hibernate
Learn how to escape SQL reserved keywords when using JPA and Hibernate. Reserved keywords can be escaped in table or column names.
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