Data Too Long for Column - Spring-Boot - spring

I am using Hibernate in my Spring application. I'm inserting the Nft object into my database. But after when i changed the "sellStatus" column to "false" or "1" it says "Data too long for column" .
Heres my entity:
#Entity
#Table(name = "nft")
#Data
public class Nft {
#Id
private Long id;
private String name;
private String qtype;
private int serial;
private Long cost;
private boolean sellStatus;
And this is the error :
ERROR 1406: 1406: Data too long for column 'sell_status' at row 1
SQL Statement:
UPDATE `nftbazaar`.`nft` SET `sell_status` = '1' WHERE (`id` = '1')

Change your sql from:
UPDATE `nftbazaar`.`nft` SET `sell_status` = '1' WHERE (`id` = '1')
To:
UPDATE `nftbazaar`.`nft` SET `sell_status` = true WHERE (`id` = '1')
OR
UPDATE `nftbazaar`.`nft` SET `sell_status` = false WHERE (`id` = '1')
Depending on your need to deal with true or false for boolean. Let the database handle your true or false to 0 or 1.

Related

DuplicateKeyException: Bulk write operation error on server:

I am new in MongoDB and trying to execute a very simple query to save collection to the database, but receive the error Bulk write operation error on server.
Entity:
#Document("role")
#Data
#NoArgsConstructor
#RequiredArgsConstructor
#EqualsAndHashCode(exclude = "id") <--------- compare only by unique field `name`
public class Role {
#Id
private String id;
#NotBlank
#NonNull
#Indexed(unique = true)
private ERole name;
}
ERole:
public enum ERole {ROLE_USER, ROLE_MODERATOR, ROLE_ADMIN}
logic:
Collection<Role> rolesFromDb = repository.findAll(); < --------- (1)
Collection<Role> rolesFromEnumSet = Arrays.stream(ERole.values()).map(Role::new).collect(Collectors.toSet());
if (!(rolesFromDb.containsAll(rolesFromEnumSet) &&
rolesFromEnumSet.containsAll(rolesFromDb))
) {
rolesFromEnumSet.removeAll(rolesFromDb); < ------------------ (2)
repository.saveAll(rolesFromEnumSet); < --------------------- (3)
}
I have a single record in the database and receive the next collection in rolesFromDatabase variable (line (1):
Role(id=63dc16b253565a43cee65848, name=ROLE_USER)
In line (2) a rolesFromEnuSet has an expected set:
0 = {Role#9387} "Role(id=null, name=ROLE_ADMIN)"
1 = {Role#9388} "Role(id=null, name=ROLE_MODERATOR)"
So, the database doesn't contain remembered records.
Why do I receive a "Duplicate error" in line (3)?
Your EqualsAndHashCode is comparing by ID, not name. This means that the rolesFromEnumSet.removeAll(rolesFromDb)) code is not removing anything in rolesFromEnumSet
This means that inside your transaction, you are actually left with 4 records:
Role(id=63dc16b253565a43cee65848, name=ROLE_USER)
Role(id=null, name=ROLE_USER)
Role(id=null, name=ROLE_MODERATOR)
Role(id=null, name=ROLE_ADMIN)
You'll receive a duplicate key error because it tries to save an additional role with the name ROLE_USER

#OneToMany field is wrongly included in JPA query

I have a small project to tinker with Spring, where I have two entities with a one to many association: 1 Restaurant -> N Dishes.
I have the following PostgreSQL schema for that:
create table if not exists restaurants (
restaurant_id uuid primary key,
name varchar(512) not null,
description varchar(1024) not null,
address varchar(512) not null,
photo_url varchar(1024)
);
create table if not exists dishes (
dish_id uuid primary key,
name varchar(512) not null,
description varchar(1024),
photo_url varchar(1024),
restaurant_id uuid references restaurants(restaurant_id) not null,
price int not null check (price > 0)
);
With the following JPA Entities:
#Entity
#Table(name = "restaurants")
class Restaurants(
#Id
var restaurantId: UUID,
var name: String,
var description: String,
var photoUrl: String?,
) {
#OneToMany(mappedBy = "restaurant")
#JoinColumn(name = "restaurant_id", nullable = false)
var dishes: MutableList<Dishes> = mutableListOf()
}
#Entity
#Table(name = "dishes")
class Dishes(
#Id
var dishId: UUID,
var name: String,
var description: String,
var photoUrl: String?,
var price: Int,
#ManyToOne(optional = false)
#JoinColumn(name = "restaurant_id", nullable = false)
var restaurant: Restaurants
)
I have defined a RestaurantsRepository as follows:
interface RestaurantsRepository: R2dbcRepository<Restaurants, UUID> {
fun findByRestaurantId(restaurantId: UUID): Mono<Restaurants>
}
The problem I'm having is that when I call findByRestaurantId I have the following exception:
org.springframework.r2dbc.BadSqlGrammarException: executeMany; bad SQL grammar [SELECT restaurants.restaurant_id, restaurants.name, restaurants.description, restaurants.photo_url, restaurants.dishes FROM restaurants WHERE restaurants.restaurant_id = $1]; nested exception is io.r2dbc.postgresql.ExceptionFactory$PostgresqlBadGrammarException: [42703] column restaurants.dishes does not exist
at org.springframework.r2dbc.connection.ConnectionFactoryUtils.convertR2dbcException(ConnectionFactoryUtils.java:235) ~[spring-r2dbc-5.3.21.jar:5.3.21]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Why is the #OneToMany field included in the SQL query?
You are trying to use Spring Data R2DBC (R2dbcRepository) in conjunction with JPA annotations. It won't work: these are two different technologies. R2DBC does not support #ManyToOne nor #JoinColumn so the annotations are simply ignored.

java jpa SqlResultSetMapping issue

I have a table form_header with 3 records
There are more fields in the table decided not to add it here in the post since most are irrelevant. I created a class/entity to get the count with distinct for each status in sql.
#Entity()
#Table(name = "Form_Header")
#SqlResultSetMapping(name = "myMapping",
entities = {#EntityResult(
entityClass = FormSummary.class,
fields = {#FieldResult(name = "status", column = "status"),
#FieldResult(name = "id", column = "header_id")})})
public class FormSummary {
#Id()
private Long id;
private String status;
<getter and setter>
with entity manager
List<FormSummary> results = entityManager.createNativeQuery("select DISTINCT(status), COUNT(header_id) as header_id from Form_Header where is_deleted = 0 group by status order by status", "myMapping").getResultList();
for (FormSummary x : results) {
System.out.println("ABC " + x.getId());
System.out.println("ABC " + x.getStatus());
}
Issue is the sysout is showing this
instead of this
status header_id
APPROVE 1
DRAFT 1
SUBMITTED 1
Whats even weird is if I add an extra record in the table with the same status
I will get the correct data in my jpa
Am I missing something in my code or a possible bug with SqlResultSetMapping?

How to generate id field value within specific range in spring data jpa

Is there any way that I can generate ID field as 4 digit number i.e from 1000 to 9999 in my Spring boot application. Current Id field looks like this:
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
#Column(name = "EMP_ID", nullable = false)
public short getEmp_id() {
return emp_id;
}
As of now id is getting generated from 1. But I wanted to get it generated starting from 1000 and incremented by 1 until 9999.
As suggest by Ishikawa in comments and by referring Sequence Generation from Sequence Generation did below changes:
#Id
#GenericGenerator(
name = "empid-sequence-generator",
strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
parameters = {
#Parameter(name = "sequence_name", value = "user_sequence"),
#Parameter(name = "initial_value", value = "1000"),
#Parameter(name = "increment_size", value = "1")
}
)
#GeneratedValue(generator = "empid-sequence-generator")
#Column(name = "EMP_ID", nullable = false)
public short getEmp_id() {
return emp_id;
}
but even after that when trying to save the emp getting the below exception:
com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'user_sequence'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:262)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1624)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:594)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:524)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7194)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2979)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:248)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:223)
NOTE: It's third party database so I can't do any schema/constraint changes.I need to handle this through java code only.
My bad. Forgot to uncomment below line in application.properties.
spring.jpa.hibernate.ddl-auto = update
After uncommenting when I reboot my application it created the "user_sequence".

Map new column from Spring Native query to entity

I have a case statement in my Native query where I am attempting to override a field in my entity.
SELECT i.id, i.ONE_TO_ONE_ID, i.ANOTHER, CASE(WHEN condition THEN 'YES' WHEN another_condition THEN 'NO' ELSE 'MAYBE' END) as word ....
I am using this with JpaRepository as a native query, with pagination.
When I run the native query against my db directly, the result set looks as though I expect.
| id_value | MAPPED_ENTITY_ID_value | another value | word_value (YES) |
When I run the native query from my JpaRepository, everything works there, except word is always null. I cant' seem to figure out how to map the additional String word result to a field in my Entity.
Is there a way to get this to map? Or will I have to create an entire #SqlResultSetMapping() for all of my fields coupled with a native query? (hoping not)
UPDATE: 1
I was generalizing above. Here is my Query.
#Query(
name = "listPagedMapping",
value = "SELECT DISTINCT i.ID, i.INSTANCE_ID, i.REGION, i.CNAME_STACK_ID, i.INSTANCE_STATE, i.IP_ADDRESS, i.EC2_ROLE_NAME, i.INSTANCE_OWNER, i.IS_MASTER, i.EC2_MASTER_ID, i.CNAME, i.EC2_START_TIMESTAMP, i.PRIVATE_DNS, i.INSTANCE_NAME, i.AUTO_TERMINATE, i.AUTO_TERMINATE_DATE, i.TERMINATION_ZONE, i.ADMIN_GROUP_AD_LDAP_ID, i.USER_GROUP_AD_LDAP_ID, (CASE WHEN i.INSTANCE_OWNER=:username THEN 'OWNER' WHEN i.ADMIN_GROUP_AD_LDAP_ID IN (g.AD_LDAP_ID) THEN 'ADMIN' WHEN i.USER_GROUP_AD_LDAP_ID IN (g.AD_LDAP_ID) THEN 'USER' END) as PERMISSION FROM USER u, USER_ACCESS_GROUPS g, EC2_PROVISIONING i WHERE i.INSTANCE_OWNER=:username and i.INSTANCE_STATE in (:instanceStates) or u.username=:username and i.INSTANCE_STATE in (:instanceStates) and g.USER_ID=u.USER_ID and (i.ADMIN_GROUP_AD_LDAP_ID IN (g.AD_LDAP_ID) or i.USER_GROUP_AD_LDAP_ID IN (g.AD_LDAP_ID))",
countQuery = "SELECT count(*) FROM (SELECT DISTINCT i.* FROM USER u, USER_ACCESS_GROUPS g, EC2_PROVISIONING i WHERE i.INSTANCE_OWNER=:username and i.INSTANCE_STATE in (:instanceStates) or u.username=:username and i.INSTANCE_STATE in (:instanceStates) and g.USER_ID=u.USER_ID and (i.ADMIN_GROUP_AD_LDAP_ID IN (g.AD_LDAP_ID) or i.USER_GROUP_AD_LDAP_ID IN (g.AD_LDAP_ID))) as ug",
nativeQuery = true)
Page<Ec2Instance> findAllByPermissionUserAdminOrOwnerAndInstanceStateIn(
#Param("username")final String username,
#Param("instanceStates") final Set<String> instanceStates,
final Pageable pageable);
}
Obviously a bit more complex.
I can get it to map to the entity field with using a named query, but then I loose all the default mappings:
#JsonInclude(JsonInclude.Include.NON_NULL)
#SuppressWarnings("unused")
#Data
#AllArgsConstructor
#NoArgsConstructor
#EqualsAndHashCode(exclude={"masterNode", "workers", "associatedBuckets"})
#Entity
#Table(name = "EC2_PROVISIONING")
#SqlResultSetMapping(
name="listPagedMapping",
columns = {
#ColumnResult(name = "permission", type = String.class)
}
)
#NamedNativeQuery(
name = "listAccessibleInstances",
query = ACCESSIBLE_QUERY,
resultSetMapping = "listPagedMapping"
)
public class Ec2Instance {
....
private String permission;
#column(name = "INSTANCE_ID")
private String instanceId;
#ManyToOne
#JoinColumn(name = "EC2_MASTER_ID")
private Ec2Instance masterNode;
#Setter(AccessLevel.NONE)
#ManyToMany(fetch = FetchType.EAGER)
#JoinTable(name = "WORKER_EC2_NODES", joinColumns = { #JoinColumn(name = "EC2_MASTER_ID") }, inverseJoinColumns = {
#JoinColumn(name = "ID") })
private Set<Ec2Instance> workers = new HashSet<>();
... More fields ..
}
I guess, I am hoping there is a way to provide a single mapping on-top of the default mapping that is done by ORM. The above code results in only a pageable of Content PERMISSION, rather than the whole entity + permission.
UPDATE: 2
Ok, so I am getting closer... Seems by removing the #ColumnResult I do get the default mapping, plus the PERMISSION field mapped over! Looks like this:
#SqlResultSetMapping(
name="listPagedMapping"
)
The last issue is it does not accept my CountQuery, and causes my tests to fail whenever a Pagination Query results with multiple pages. Looks like Spring try's to come up with its own CountQuery, which is not correct.
UPDATE: 3
To finish this off, looks like I can provide the Count Query as described here: Spring Data - Why it's not possible to have paging with native query
I will give this a go and update back.
I never got this to work quite how I wanted. I am sure I could by mapping my entire entity, but, that would have been painstaking. I ended up solving this by using NamedNativeQueries, with mapping for the additional Column as a result of my Case statement. My entity class is now annotated like:
#JsonInclude(JsonInclude.Include.NON_NULL)
#SuppressWarnings("unused")
#Data
#AllArgsConstructor
#NoArgsConstructor
#EqualsAndHashCode(callSuper = false)
#Entity
#Table(name = "EC2_PROVISIONING")
#SqlResultSetMappings({
#SqlResultSetMapping(
name = "listPagedMapping",
entities = {
#EntityResult(
entityClass = Ec2Instance.class
)
},
columns = {#ColumnResult(name = "permission", type = String.class)}
),
#SqlResultSetMapping(name = "listPagedMapping.count", columns = #ColumnResult(name = "cnt"))
})
#NamedNativeQueries({
#NamedNativeQuery(
name = "Ec2Instance.listAccessibleInstances",
query = ACCESSIBLE_QUERY,
resultClass = Ec2Instance.class,
resultSetMapping = "listPagedMapping"
),
#NamedNativeQuery(
name = "Ec2Instance.listAccessibleInstances.count",
resultSetMapping = "listPagedMapping.count",
query = ACCESSIBLE_QUERY_COUNT
)
})
We also dont need the permission field in this entity anymore. I removed that.
Then in my Repository:
Page<Object[]> listAccessibleInstances(
#Param("username")final String username,
#Param("instanceStates") final Set<String> instanceStates,
final Pageable pageable);
Thats it! Now the result of my case statement is returned with each entity.
Object[0] = original, default mapped entity.
Object[1] = permission

Resources