I have multiple entities annotated with the following:
#TableGenerator(name = "XXX_Gen", table = "XXX_GEN", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL")
#GeneratedValue(strategy = GenerationType.TABLE)
#Id
private String id;
I am using MySQL behind the scenes with eclipselink and the problem is that regardless of the values that I enter in the name and table MySQL always just uses a single table called 'SEQUENCE' to increment the PK's values.
Normally this isn't an issue except I have a specific case where I need an entity to have its own incremental sequence.
I wasn't specifying the generator! It should be;
#TableGenerator(name = "XXX_Gen", table = "XXX_GEN", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL")
#GeneratedValue(strategy = GenerationType.TABLE, generator="XXX_Gen")
#Id
private String id;
I think I need another cup of coffee!
Related
I would like to set indexes on multiple columns within a single table in MySQL database. After reading this article, I'm not 100% sure which approach to use.
So my (simplified) table looks like this:
#Data
#Entity
#SuperBuilder
#NoArgsConstructor
#AllArgsConstructor
#Table(name = "loan")
public class Loan {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "loan_id", unique = true, nullable = false)
private long id;
#Column(name = "amount", unique = false, nullable = false)
private double amount;
#Column(name = "rate", unique = false, nullable = false)
private double rate;
#Column(name = "payments", unique = false, nullable = false)
private int payments;
#Column(name = "pmt", unique = false, nullable = false)
private double pmt;
}
I will have a lot of search queries, for instance:
SELECT *
FROM Loan loan
WHERE loan.amount =: amount
AND loan.rate =: rate
AND loan.payments =: payments
AND loan.pmt =: pmt
LIMIT 1;
Now, I would like to index fields in WHERE clause. Essentially, I would like to achieve effect of a "composite key" where in table loan there are only unique combinations of mentioned fields. So I cannot have two rows all with some values.
Is there such a configuration?
ou can add a UNIQUE constraint, which would be indexed automatocally
#Table(uniqueConstraints =
{
#UniqueConstraint(name = "UniqueWhereclause", columnNames = { "amount", "rate","payments","pmt" })})
or you can create an index alone
#Entity
#SuperBuilder
#NoArgsConstructor
#AllArgsConstructor
#Table(name = "loan", indexes = {
#Index(columnList = "amount, rate,payments,pmt", name = "name_idx") })
DOUBLE is likely to cause trouble for you. Switch to DECIMAL with a suitable number of decimal places -- so that = can be tested correctly.
Also, that's a strange combination of 4 things to use in the WHERE. Any 3 of those columns should (mathematically) determine the value for the 4th.
Does the table also have the name of the person taking out the loan? Or is this table a list of possible loans? As rates change, so you add more rows to the table? But why have the table if 3 columns can be used to compute the 4th?
I am unable to reset the id of the objects that I am adding to SQL, through JPA. I tried to write a native query to truncate when I use my deleteAll method, which works, no errors, but the autogenerated id is still not 1.
I tried "ALTER TABLE task AUTO_INCREMENT = 1" after deleting, same result.
My method used for resetting
#Transactional
#Modifying
#Query(value = "ALTER TABLE task AUTO_INCREMENT = 1", nativeQuery = true)
void resetId();
My entity id
public class Task {
#Id
#Column(name = "id", nullable = false)
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
How can I create a Single Entity from two other tables/Entity in JPA/hibernate making a INNER JOIN result with a common key. I was using the below code but it gives me a full join instead of an inner join. it give me records from the meal table even if the
"id 1" does not exist in the allergies table, example:
{id=1, name='tacos', description='Mexican food', price ='10',peanuts=null, celery=null, sesameSeeds=null}
How can constrain to don't return any records if the 'id' is missing from the secondary table allergies? to show only records when the primary key is present in both tables.
I want something like this instead:
{id=1, name='tacos', description='Mexican food', price ='10',peanuts='no', celery='no', sesameSeeds='no'}
Please advise.
#Entity
#Table(name = "meal")
#SecondaryTable(name = "allergens", pkJoinColumns = #PrimaryKeyJoinColumn(name = "meal_id"))
class Meal {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
Long id;
#Column(name = "name")
String name;
#Column(name = "description")
String description;
#Column(name = "price")
BigDecimal price;
#Column(name = "peanuts", table = "allergens")
boolean peanuts;
#Column(name = "celery", table = "allergens")
boolean celery;
#Column(name = "sesame_seeds", table = "allergens")
boolean sesameSeeds;
// standard getters and setters
}
This question relates to managing id numbers to ensure that I don't end up with duplicate identities on my person's table. It is a springboot project with MySQL database.
Some background:
I have an HTML form for submitting an "episode". Each episode contains "persons" and has a relationship to "persons" of ManyToMany.
"episodes" are entered and submitted into the database (db1) by field staff. A few hours later the episode is manually entered into a second database (db2) by BackOffice staff.
On my spring attached database (db1) I have a persons table which has a native auto generated id field. db1 also has a id2 field - which records the unique id for the person from db2.
Field staff do not always have access to id2 when they enter a episode, but BackOffice staff do.
When I save a new "episode" I need the save method to check if person id2 exists in the database and perform an update on person (not create new).
Then delete the duplicate person.
Episode entity:
#Entity
public class Episode {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
#Column(name="start_date")
#DateTimeFormat (pattern="dd/MM/yyyy HH:mm")
private Date start_date;
#ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
#JoinTable(name = "episode_person", joinColumns = #JoinColumn(name = "episode_id", referencedColumnName = "id"), inverseJoinColumns = #JoinColumn(name = "person_id", referencedColumnName = "id"))
private List<Person> persons;
#OneToOne(cascade=CascadeType.ALL)
//#JoinColumn(name = "id")
private Address address;
Person Entity
#Entity
public class Person {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long id2;
private String surname;
private String firstname;
private String phoneHome;
#DateTimeFormat(pattern = "dd/mm/yyyy")
private Date dob;
#ManyToMany(mappedBy = "persons", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private List<Episode> episodes;
EpisodeServiceImpl
#Override
#Transactional
public Episode saveEpisode(Episode episode) {
List mergedPeople = personService.mergeDetachedWithAttached( episode.getPersons() );
episode.setPersons( mergedPeople );
return episodeRepository.save(episode);
}
PersonServiceImpl
#Transactional
#Override
public List mergeDetachedWithAttached(List<Person> people) {
final List<Person> results = new ArrayList<>();
if ( people != null && !people.isEmpty() ) {
// loop over every person
for (Person person : people) {
// for current person try to retrieve from db via Id2
Person db2Person = personRepository.findById2( person.getId2() );
// if a person was retrieved use them instead of submitted person
if (db2Person != null ) {
System.out.println("A matching person was found in the db using id2 - this is the person that will be added");
results.add(db2Person);
} else {
results.add(person);
}
}
}
return results;
The way this is written at the moment when ever a new episode is submitted I create new person(s) even if I successfully looked them up from db1 using id2 and added them to the episode.
How can I handle this so that:
I can merge duplicate identities based on comparing id2. The joining table that holds episode_id and person_id will also need to be updated where a id is deleted after a merge.
It's much easier if you replace the #ManyToMany association with 2 bidirectional #OneToMany associations, meaning that you map the association table as well.
This way, considering that you have those duplicated Episode or Person entities, you can easily move the joined association by simply adjusting the #ManyToOne associations on the association entity. As for the duplicated Episode or Person you can either use UPSERT as explained below, or do a manual merging after the entries are added to the DB by the batch process.
Typically, when you have multiple nodes that running concurrently, you can use the database UPSERT or MERGE operation.
You could combine the UPSERT with Hibernate #SqlInsert annotation.
To handle the FK updates, you'd need to use the FK ON DELETE CASCADE strategy.
I am using Sprint Boot, Sprint Data JPA, Hibernate 4 and MySQL. I have a table with an autoincrement column. I can insert new entities ok and the column auto-increments. Sometimes though I want to specify the ID when doing an insert but Hibernate appears to ignore any ID value I set.
Table definition:
#Entity
#Table(name = "example")
public class Example implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "id")
private Integer id;
... more columns ....
}
How can I have both auto-increment when no id is set AND allow setting of the ID by my application?
Its because your ID is not that will be stored in the database,rather than that it is considering #GeneratedValue(strategy = GenerationType.IDENTITY) which is generating values for you