Have a class.
#Entity
#Table(name="sessions")
#Component
public class Session {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#ElementCollection
private Set<String> addedWords;
}
My flow is: get session one time. Add one word to addedWords and save session, repeat it. The problem that when I check my database, there are a lot of duplications, at the same time I don't have any duplications in Java class. So the Set<String> addedWords in class and this words in database not the same. Why I have this strange behavior and how to make things work well without any duplication? I use PostgreSQL. Saving method:
public synchronized void addWord(Session session, String word) {
session.getAddedWords().add(word);
sessionRepository.save(session);
}
Here spring config
spring:
jpa:
show-sql: true
hibernate:
ddl-auto: update
Related
I have a Spring Boot application using Spring Data REST. I have a domain entity called User with a boolean field isTeacher. This field has been already setup by our DBA in the User table with type bit and a default value of 1:
#Data
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; // This Id has been setup as auto generated in DB
#Column(name = "IS_TEACHER")
private boolean isTeacher;
}
And the User repository:
public interface UserRepository extends CrudRepository<User, Long>{
}
I was able to add a new user by giving the below request and POST to http://localhost:8080/users, a new user was created in the DB having isTeacher value 1:
{
"isTeacher" : true
}
However, when I tried to change IS_TEACHER by giving PATCH (or PUT) and this request:
{
"isTeacher" : false
}
The response showed that "isTeacher" is still true and the value didn't get changed in the table either. Can someone please let me know why this is happening?
The issue is due to #Data annotation of lombok is ignoring if you have a field that start with isXx it generates getters and setters to boolean with isTeacher for getters and setTeacher for setters then you are not able to update correctly your property, if you put "teacher" when updating should work but you should solve this by overriding that setter.
#Setter(AccessLevel.NONE) private boolean isTeacher;
public void setIsTeacher(boolean isTeacher) {
this.isTeacher = isTeacher;
}
Looking for next debugging steps. I'm using Hibernate with Spring Boot, with my ddl setting set to update (I realize the hibernate setting is redundant but I just wanted to be sure):
application.properties
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
hibernate.hbm2ddl.auto=update
I've just added a new Entity, which succesfully created. But when adding an additional field, profileImageURI2, nothing is ever run to attempt to add it:
#Entity
#Data
public class User {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String profileImageURI;
private String profileImageURI2;
}
Is there any additional config that will control the modification of existing tables? The user also has all permissions for the database.
i've spring and hibernate project in that i configured audit for Table and its working fine but my problem is i want to avoid audit at time of creating new record but while doing updating it should audit below my code
Entity:
#Entity
#Table(name = "building")
#Audited
public class BuildingClass extends CommonTableFields {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "BID")
#JsonProperty
private long id;
#JsonProperty
private String username;
#JsonProperty
private double count;
//getters and setters
}
implementation:
// here i don't want to audit
#Override
public void save(BuildingClass buildingclass) {
repo.save(buildingclass)
}
// here i want to audit
#Override
public void update(BuildingClass buildingclass) {
repo.save(buildingclass)
}
thanks in advance
It is going to depend on what strategy your using for auditing.
The DefaultAuditStrategy should work by simply not registering post-insert event listener. You would do this by registering your own envers integrator that doesn't register that specific event handler.
The ValidityAuditStrategy will be a bit of a problem. The problem here is that this strategy performs a set of update operations internally when a row is modified and those operations expect the initial insert audit row to exist and will fault if it does not.
You could override this strategy with a custom one that disables this check, but understand the check was added to detect data issues with the audit rows rather than using assumptions.
But the key to all this is conditional auditing, see reference documentation for information.
There is a problem about generating id while persisting into database.
I added the following code to my jpa entity file, however I'm getting 0 for personid.
#Id
#Column(unique=true, nullable=false, precision=10, name="PERSONID")
#SequenceGenerator(name="appUsersSeq", sequenceName="SEQ_PERSON", allocationSize=1)
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "appUsersSeq")
private long personid;
EjbService:
#Stateless
public class EjbService implements EjbServiceRemote {
#PersistenceContext(name = "Project1245")
private EntityManager em;
#Override
public void addTperson(Tperson tp) {
em.persist(tp);
}
}
0 is default value for long type. The id will be set after invoking select query for the related sequence, which commonly is executed when you persist the entity. Are you persisting the entity? In case yes, post the database sequence definition to check it.
I'm using EclipseLink 2.1.3 with a container managed EntityManager to interface with an Oracle 11g DB.
I want to have an Entity's #Id variable updated immediately after I call persist() on the EM.
What is the correct strategy to do so with an Oracle DB?
None of the examples I've found on this site deal with this problem with container managed persistence.
The Entity looks like this:
#Entity
#Table(name = "ANNOUNCEMENT_DELIVERY_LOG")
public class AnnouncementDeliveryLog implements Serializable {
#Id
private BigDecimal id;
#ManyToOne
#JoinColumn(name = "ANNOUNCEMENT_ID ")
private Announcements announcement;
public AnnouncementDeliveryLog() {
}
}
Do I need to add something like the following?
#Column(nullable = false)
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ANNOUNCEMENT_DELIVERY_LOG_SEQ")
#SequenceGenerator(name="ANNOUNCEMENT_DELIVERY_LOG_SEQ", sequenceName="ANNOUNCEMENT_DELIVERY_LOG_SEQ")
To persist the Entity I'm just calling persist(). Do I also need to call flush()?
Yes, you have to provide a #SequenceGenerator annotation in order that JPA automatically assigns a new ID to the entity during persist().
A flush is not necessary.