Spring Boot- Duplicate entry for key 'PRIMARY' - mysql

I am new to Spring Boot. I am trying to use the save() functionality via the JPA library using Postman for the first time. My database is a legacy Mysql database. Generically speaking, this table contains data of baseball players who have been drafted into a fantasy baseball league. The primary key of my table is 'play_id', and I also track the player's 'mlb_id' (Major League Baseball's unique identifier) in the same table.
Here is my code:
Table setup in Mysql:
CREATE TABLE `mlb_rosters` (
`play_id` int(10) NOT NULL,
`mlb_id` int(10) NOT NULL,
`name_first` varbinary(255) NOT NULL,
`name_last` varbinary(255) NOT NULL,
`bats` varchar(1) NOT NULL,
`throws` varchar(1) NOT NULL,
`birthday` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `mlb_rosters`
ADD PRIMARY KEY (`play_id`),
ADD UNIQUE KEY `mlb_id` (`mlb_id`),
ADD UNIQUE KEY `mlb_id_2` (`mlb_id`);
ALTER TABLE `mlb_rosters`
MODIFY `play_id` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6730;
I also ran insert statements for approximately ~1500 players, so this is not a blank table.
My object in Springboot:
package com.example.demo.entities;
import javax.persistence.*;
#Entity
#Table(name="mlb_rosters")
public class IbcMlbPlayer {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name="play_id", columnDefinition = "int(10)")
private Integer playId;
#Column(name="mlb_id")
private Integer mlbId;
#Column(name="name_first", columnDefinition = "varbinary(255)")
private String nameFirst;
#Column(name="name_last", columnDefinition = "varbinary(255)")
private String nameLast;
#Column(name="bats")
private String bats;
#Column(name="throws")
private String thrws;
#Column(name="birthday")
private String birthday;
public IbcMlbPlayer(){
}
public Integer getPlayId() {
return playId;
}
public void setPlayId(Integer playId) {
this.playId = playId;
}
public Integer getMlbId() {
return mlbId;
}
public void setMlbId(Integer mlbId) {
this.mlbId = mlbId;
}
public String getNameFirst() {
return nameFirst;
}
public void setNameFirst(String nameFirst) {
this.nameFirst = nameFirst;
}
public String getNameLast() {
return nameLast;
}
public void setNameLast(String nameLast) {
this.nameLast = nameLast;
}
public String getBats() {
return bats;
}
public void setBats(String bats) {
this.bats = bats;
}
public String getThrws() {
return thrws;
}
public void setThrws(String thrws) {
this.thrws = thrws;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
The relevant path of my controller:
#PostMapping(value = "/saveIbcMlbPlayer")
public IbcMlbPlayer saveIbcMlbPlayer(#RequestBody IbcMlbPlayer ibcMlbPlayer){
return ibcMlbPlayerDao.save(ibcMlbPlayer);
}
My Dao:
package com.example.demo.dao;
import com.example.demo.entities.IbcMlbPlayer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface IbcMlbPlayerDao extends JpaRepository<IbcMlbPlayer, Integer> {
}
When I attempt to do a Post request to the save path and pass in the JSON object of the player who I'm attempting to create, I get the following error:
Duplicate entry '25' for key 'PRIMARY'
In this case, I've tried this 25 times, so Postman/Spring Boot keep incrementing the 'play_id' field by 1 (this number goes up in the error message by one each time I test).
I understand the error, for whatever reason, Spring Boot isn't getting the max value of the 'play_id' field, incrementing it by one, and then attempting to do the insert. I would have expected 'play_id' to be 6730, which I believe is the table's max play_id plus one. Does anyone know how to fix this? Any help would be really appreciated!

AUTO shouldn't be used as GenerationType you must use IDENTITY
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="play_id", columnDefinition = "int(10)")
private Integer playId;

Related

How to update only few fields of entity using JPA and Hibernate?

I'm using MySQL DB.
My entity for the table is Account with the following fields:
id(long), balance (double), created_on(Date), currency(Enum).
When I'm doing a PUT request to update the account, I pass in the request body JSON.
I want to update, for example, only the balance, but the other columns' values to be saved.
In that case (I'm not passing the currency type) the balance is updated, but the currency has value NULL. Is that because it's enum?
I've tried using #DynamicUpdate annotation, but still, it doesn't have any change.
#RestController
public class AccountController {
#PutMapping("/accounts/{id}")
public void updateAccount(\#PathVariable long id, #RequestBody AccountDto accountDto) {
accountService.updateAccount(id, accountDto);
}
}
I'm using AccountDto (which I pass in the request body) and I'm calling the accountService
public void updateAccount(long id, AccountDto accountDto) {
Account account = accountRepository.getOne(id);
account.fromDto(accountDto);
this.accountRepository.save(account); }),
which calls the AccountRepository
public void fromDto(AccountDto accountDto) {
this.balance = accountDto.getBalance();
this.currency = accountDto.getCurrency();
}
Here is the AccountDto class:
public class AccountDto {
private long id;
#NotNull #PositiveOrZero
private double balance;
#NotNull #Enumerated(EnumType.STRING)
private Currency currency;
}
You need to perform a select query on Account entity and then update only the desired fields.
(Eg - making assumptions of my own of underlying method being used for accessing DB)
public updateAccount(AccountModel jsonBody) {
Account entity = accountRepository.findById(jsonBody.getAccountId());
entity.setBalance(jsonBody.getBalance());
accountRepository.save(entity);
}
If you get null as currency in the JSON you shouldn't update it:
So fromDto must look like:
public void fromDto(AccountDto accountDto) {
this.balance = accountDto.getBalance();
if (accountDto.getCurrency() != null) {
this.currency = accountDto.getCurrency();
}
}

Dynamically Changing Table Names

I am currently working with a database that has read and write tables.
There are always two tables with the same schema, distinguished by a number as suffix, eg. table1 and table2.
Now, there is another source where I get the current number from. I have to use this number to select from the corresponding table with the matching suffix.
Right now, for every table i have a #MappedSuperclass containing the schema and two implementation classes specifying the table name via #Table(name = "..1") and #Table(name = "..2").
This solution works but by now I discovered a lot of drawbacks and fear there will be many more. Is there another, better way to solve this?
Unfortunately, I could not find out what this kind of database mechanism is called hence I could not find any other sources on the internet.
Thank you in advance!
The most obvious solution:
if ( num == 1 )
{
Table1 table1 = createTable1();
table1.set...;
entityManager.persist( table1 );
} else
{
Table2 table2 = createTable2();
table2.set...;
entityManager.persist( table2 );
}
Or with constructor calling by name (with Lombok annotations):
#Entity
#Data
public class CommonBase
{}
#Entity
#Data
public class Table1 extends CommonBase
{}
#Entity
#Data
public class Table2 extends CommonBase
{}
#Stateless
#LocalBean
public class CommonBaseBean
{
#Inject
private CommonBaseBUS commonBaseBUS;
protected void clientCode()
{
Table0 t0 = (Table0) commonBaseBUS.createEntityByIndex( 0 );
t0.set...();
commonBaseBUS.persisEntity( t0 );
Table1 t1 = (Table1) commonBaseBUS.createEntityByIndex( 1 );
t1.set...();
commonBaseBUS.persisEntity( t1 );
}
}
#Dependent
class CommonBaseBUS
{
#Inject
private CommonBaseDAL commonBaseDAL;
#Setter
private String entityBaseName = "qualified.path.Table";
public CommonBase createEntityByIndex( int index_ ) throws ClassNotFoundException
{
String entityName = entityBaseName + Integer.toString( index_ );
return createEntityByName( entityName );
}
public void persisEntity( CommonBase cb_ )
{
commonBaseDAL.persistEntity( cb_ );
}
protected CommonBase createEntityByName( String entityName_ ) throws ClassNotFoundException
{
Class<?> c = Class.forName( entityName_ );
try
{
return (CommonBase) c.newInstance();
}
catch ( InstantiationException | IllegalAccessException ex )
{
throw new ClassNotFoundException();
}
}
}
#Dependent
class CommonBaseDAL
{
#PersistentContext
private EntityManager em;
public void persisEntity( CommonBase cb_ )
{
em.persistEntity( cb_ );
}
}

Hibernate generating incorrect MySQL script

I have two Classes mapping each one to an Entity in MySQL database. Whenever I try to map into to DB I got an MySQL Error
Class Owner:
#Entity
public class Owner {
#Id #GeneratedValue
private int idOwner;
public int getIdOwner() {
return idOwner;
}
public void setIdOwner(int idOwner) {
this.idOwner = idOwner;
}
}
Class Car with FK:
#Entity
public class Car {
#Id #GeneratedValue
private int idCar;
#ManyToOne
#JoinColumn( name = "idOwner")
private Owner owner;
public int getIdCar() {
return idCar;
}
public void setIdCar(int idCar) {
this.idCar = idCar;
}
public Owner getOwner() {
return owner;
}
public void setOwner(Owner owner) {
this.owner = owner;
}
}
Code running:
EntityManagerFactory f = Persistence.createEntityManagerFactory("glorious");
EntityManager em = f.createEntityManager();
EntityTransaction t = em.getTransaction();
t.begin();
Car c = new Car();
Owner o = new Owner();
c.setOwner(o);
em.persist(c);
em.persist(o);
t.commit();
f.close();
em.close();
Error:
GRAVE: Unsuccessful: alter table .Car add index FK107B43F620606 (idOwner), add constraint FK107B43F620606 foreign key (idOwner) references .Owner (idOwner)
27/05/2014 20:35:01 org.hibernate.tool.hbm2ddl.SchemaExport create
GRAVE: Can't create table 'glorious.#sql-2aa_1f8' (errno: 150)
MySQL Version : 5.5.34
Engine : InnoDB
Hibernate Dialect : MySQL5InnoDBDialect
I took the script generated by Hibernate and tested it directly in phpMyAdmin, it didn't work.
SQL script :
alter table .Car add index FK107B43F620606 (idOwner), add constraint FK107B43F620606 foreign key (idOwner) references .Owner (idOwner)
If I fix the script by replacing the tables name with , e.g. Car or glorious.Car than it works. Anyone has an idea?
Solved it. My persistence.xml had the property default_schema set to empty. That's why no DB name was appearing before the table's name.

The most efficient way to store photo reference in a database

I'm currently looking to store approximately 3.5 million photo's from approximately 100/200k users. I'm only using a mysql database on aws. My question is in regards to the most efficient way to store the photo reference. I'm only aware of two ways and I'm looking for an expert opinion.
Choice A
A user table with a photo_url column, in that column I would build a comma separated list of photo's that both maintain the name and sort order. The business logic would handle extracting the path from the photo name and append photo size. The downside is the processing expense.
Database example
"0ea102, e435b9, etc"
Business logic would build the following urls from photo name
/0e/a1/02.jpg
/0e/a1/02_thumb.jpg
/e4/35/b9.jpg
/e4/35/b9_thumb.jpg
Choice B - Relational Table joined on user table with the following fields. I'm just concerned I may have potential database performance issues.
pk
user_id
photo_url_800
photo_url_150
photo_url_45
order
Does anybody have any suggestions on the better solution?
The best and most common answer would be: choice B - Relational Table joined on user table with the following fields.
id
order
user_id
desc
photo_url_800
photo_url_150
photo_url_45
date_uploaded
Or a hybrid, wherein, you store the file names individually and add the photo directory with your business logic layer.
My analysis, your first option is a bad practice. Comma separated fields are not advisable for database. It would be difficult for you to update these fields and add description on it.
Regarding the table optimization, you might want to see these articles:
Optimizing MyISAM Queries
Optimizing InnoDB Queries
Here is an example of my final solution using the hibernate ORM, Christian Mark, and my hybrid solution.
#Entity
public class Photo extends StatefulEntity {
private static final String FILE_EXTENSION_JPEG = ".jpg";
private static final String ROOT_PHOTO_URL = "/photo/";
private static final String PHOTO_SIZE_800 = "_800";
private static final String PHOTO_SIZE_150 = "_150";
private static final String PHOTO_SIZE_100 = "_100";
private static final String PHOTO_SIZE_50 = "_50";
#ManyToOne
#JoinColumn(name = "profile_id", nullable = false)
private Profile profile;
//Example "a1d2b0" which will later get parsed into "/photo/a1/d2/b0_size.jpg"
//using the generatePhotoUrl business logic below.
#Column(nullable = false, length = 6)
private String fileName;
private boolean temp;
#Column(nullable = false)
private int orderBy;
#Temporal(TemporalType.TIMESTAMP)
private Date dateUploaded;
public Profile getProfile() {
return profile;
}
public void setProfile(Profile profile) {
this.profile = profile;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public Date getDateUploaded() {
return dateUploaded;
}
public void setDateUploaded(Date dateUploaded) {
this.dateUploaded = dateUploaded;
}
public boolean isTemp() {
return temp;
}
public void setTemp(boolean temp) {
this.temp = temp;
}
public int getOrderBy() {
return orderBy;
}
public void setOrderBy(int orderBy) {
this.orderBy = orderBy;
}
public String getPhotoSize800() {
return generatePhotoURL(PHOTO_SIZE_800);
}
public String getPhotoSize150() {
return generatePhotoURL(PHOTO_SIZE_150);
}
public String getPhotoSize100() {
return generatePhotoURL(PHOTO_SIZE_100);
}
public String getPhotoSize50() {
return generatePhotoURL(PHOTO_SIZE_50);
}
private String generatePhotoURL(String photoSize) {
String firstDir = getFileName().substring(0, 2);
String secondDir = getFileName().substring(2, 4);
String photoName = getFileName().substring(4, 6);
StringBuilder sb = new StringBuilder();
sb.append(ROOT_PHOTO_URL);
sb.append("/");
sb.append(firstDir);
sb.append("/");
sb.append(secondDir);
sb.append("/");
sb.append(photoName);
sb.append(photoSize);
sb.append(FILE_EXTENSION_JPEG);
return sb.toString();
}
}

DataNucleus JDO on MySQL fails with FK violation on 1:1 relations

I have issues persisting a simple 2 classes on DataNucleus 3.1.3 on MySQL, where DataNucleus seems to create invalid foreign-keys, ending up in a "foreign key constraint fails" -exception from database.
Here my classes:
// datastore since i dont care about identity here
#PersistenceCapable(identityType = IdentityType.DATASTORE)
class A {
#Persistent
int x;
#Persistent
int y;
}
// identity type:application here to enable id lookups
#PersistenceCapable
class B {
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.NATIVE)
long id;
#Persistent
double longitude;
#Persistent
double latitude;
// simple 1:1 unidirectional
#Persistent
A a;
}
The schemaTool created the tables (InnoDB) which looks good, but an insert fails, here the logs:
12:54:11,369 DEBUG [DataNucleus.Datastore.Native] - INSERT INTO `A` (`X`,`Y`) VALUES (<1>,<1>)
12:54:11,387 DEBUG [DataNucleus.Datastore.Persist] - Execution Time = 18 ms (number of rows = 1)
12:54:11,398 DEBUG [DataNucleus.Datastore.Persist] - Object "foo.A#624af1e" was inserted in the datastore and was given strategy value of "3"
12:54:11,403 DEBUG [DataNucleus.Datastore] - Closing PreparedStatement "org.datanucleus.store.rdbms.ParamLoggingPreparedStatement#6f5ba238"
12:54:11,404 DEBUG [DataNucleus.Datastore.Native] - INSERT INTO `B` (`LONGITUDE`,`LATITUDE`,`A_A_ID_OID`) VALUES (<0.5099776394799052>,<0.6191090630996077>,<51>)
12:54:11,419 WARN [DataNucleus.Datastore.Persist] ... Cannot add or update a child row: a foreign key constraint fails (`xperimental`.`B`, CONSTRAINT `B_FK1` FOREIGN KEY (`A_A_ID_OID`) REFERENCES `A` (`A_ID`))
Looking at the logs on lines (3) and (5) its very suspicious that an insert into table A returned a PK of "3" but DataNucleus instead uses a value of "51" as FK on A when inserting data into table B which causes the violation.
Where is the issue? Thanks
UPDATE: the resources
Class A
package jdotest.a;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
#PersistenceCapable(identityType = IdentityType.DATASTORE)
public class A {
#Persistent
private int x;
#Persistent
private int y;
public int getX() {
return x;
}
public int getY() {
return y;
}
}
Class B
package jdotest.b;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import jdotest.a.A;
#PersistenceCapable
public class B {
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.NATIVE)
long id;
#Persistent
double longitude;
#Persistent
double latitude;
// simple 1:1 unidirectional
#Persistent
A a;
public long getId() {
return id;
}
public double getLongitude() {
return longitude;
}
public double getLatitude() {
return latitude;
}
public void setA(A a) {
this.a = a;
}
public A getA() {
return a;
}
}
Dao
package dao;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Transaction;
import jdotest.b.B;
public class BDao {
public void write(B b) {
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("cloud-sql");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(b);
tx.commit();
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
}
}
execution
package exec;
import jdotest.a.A;
import jdotest.b.B;
import dao.BDao;
public class Ex{
public void persist(){
A a = new A();
B b = new B();
b.setA(a);
new BDao().write(b); //<-- exception
}
}
the exception *
java.sql.SQLException: Cannot add or update a child row: a foreign key constraint fails (xperimental.b, CONSTRAINT B_FK1 FOREIGN KEY (A_A_ID_OID) REFERENCES a (A_ID))