I'm having an Entity with an embedded Entity in spring boot.
Now when my embedded entity has changes and I query my Entity for changes the changes are not in the change list.
#Entity
#Data
#AllArgsConstructor
#NoArgsConstructor
public class TaskEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Embedded
private LockSettingsEmbeddable lockSettings;
}
#Embeddable
#Data
#AllArgsConstructor
#NoArgsConstructor
public class LockSettingsEmbeddable {
private boolean locked;
#Enumerated(EnumType.STRING)
private TaskLock lockSetting;
}
List<Change> changes = javers.findChanges(QueryBuilder.byInstanceId(taskId, TaskEntity.class).build());
List<Change> modifiableList = new ArrayList<>(changes);
modifiableList.sort((o1, o2) -> -1 * o1.getCommitMetadata().get().getCommitDate().compareTo(o2.getCommitMetadata().get().getCommitDate()));
return javers.getJsonConverter().toJson(modifiableList);
Just enable the withChildValueObject filter, see
https://javers.org/documentation/jql-examples/#child-value-objects-filter
Related
I cant set my id to null. because modelMapper's skip() method always return null. I don't know how to fix it. I trying to convert dto to entity.
I trying to convert dto to entity but
SubSectionGroupOptionsEntity skip = skip();
always return null. I am using some configuration.
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.LOOSE).setAmbiguityIgnored(true).setDeepCopyEnabled(true).setSkipNullEnabled(true);
modelMapper.addMappings(new PropertyMap<SubSectionGroupOptionsDTO, SubSectionGroupOptionsEntity>() {
#Override
protected void configure() {
SubSectionGroupOptionsEntity skip = skip();
skip.setId(null);
}
});
QualitySectionMasterEntity qualitySectionMasterEntity = modelMapper.map(sectionSaveDTO, QualitySectionMasterEntity.class);
Look here's my SubSectionGroupOptionsDTO class :
#AllArgsConstructor
#ToString
#Builder
#Data
#NoArgsConstructor
public class SubSectionGroupOptionsDTO {
private Long id;
private String optionName;
private String inputType;
private List<SubSectionGroupOptionActionsDTO> quesAnsGrpOptionAction;
}
and my entity class is :
#NoArgsConstructor
#AllArgsConstructor
#Data
#ToString
#Builder
#Entity
#Table(name = "sub_section_group_options")
public class SubSectionGroupOptionsEntity {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "option_name")
private String optionName;
#Column(name = "input_type")
private String inputType;
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
#JoinColumn(name = "sub_section_group_options_id")
private List<SubSectionGroupOptionActions> subSectionGroupOptionActionsSet;
}
Remember ::: SubSectionGroupOptionsEntity is a deep sub child of
QualitySectionMasterEntity
please look and provide a fixed solution :
I'm currently working on developing a recipe application and I'm having trouble with DB table generation.
Here are the Entity files I'm using:
// Recipe.java
#Data
#Entity
#AllArgsConstructor
#NoArgsConstructor
#Table(name = "recipes")
public class Recipe {
#Id
#GeneratedValue
private int id;
private String name;
private String description;
private String instruction;
#ManyToOne
private User user;
#OneToMany(cascade=CascadeType.ALL)
private List<RecipeIngredient> ingredients = new ArrayList<>();
}
// Ingredient.java
#Data
#Entity
#AllArgsConstructor
#NoArgsConstructor
#Table(name = "ingredients")
public class Ingredient {
#Id
#GeneratedValue
private int id;
private String name;
}
// RecipeIngredient.java
#Data
#Entity
#AllArgsConstructor
#NoArgsConstructor
public class RecipeIngredient {
#Id
#GeneratedValue
private int id;
#ManyToOne
private Ingredient ingredient;
private String amount;
}
Spring Boot Automatically creates tables for me but I just wanna have one table for RecipeIngredient, but it creates two tables for them.
It works perfectly fine but the thing I want is just how to make these two tables into one or make spring boot not generate one of them.
If you want recipe_ingedients table only delete recipeIngredient Entity Class and if you want to keep recipe_ingredient table remove this:
#OneToMany(cascade=CascadeType.ALL)
private List<RecipeIngredient> ingredients = new ArrayList<>();
I am using jpa and have 2 entities, but in this situation :
Entity A = schemaA.tableX
Entity B = schemaB.tableX
tableX is the same but duplicated on 2 different schemas (A and B), same columns inside, and I am supposed to fill them with same data through my application.
The question is : is it possible in my code, to mappe once this tableX and somehow, data will be splited on two, one for each schema ??
I do not want :
#Entity
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
#Table(name = "TableX", schema = "A")
public class A implements Serializable {
#Id
#Column(name = "id")
private String id; <=== on table A
}
And :
#Entity
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
#Table(name = "TableX", schema = "B")
public class B implements Serializable {
#Id
#Column(name = "id")
private String id; <=== on table B
}
A dumb copy/past of : entity, repository, service, impl... because exactelly the same Table !
You could do that probably with a #MappedSuperClass:
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
#MappedSuperClass // <-- see this annotation
public class AbstractTable implements Serializable { // <-- name the class however you want
#Id
#Column(name = "id")
private String id;
}
And then change your two other classes to this:
#Entity
#Table(name = "TableX", schema = "A")
public class A extends AbstractTable {}
And
#Entity
#Table(name = "TableX", schema = "B")
public class B extends AbstractTable {}
For example, I have a Job entity and a worker entity.
when I add new job I want to assign workers to it but instead of adding it's users to
the database again ( and create duplicates ) I want to just update workers
( add jobs to them when I create a new job with worker list )
example :
#Entity
#Table(name = "job")
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
public class Job{
#Id
#Column(name = "id")
private Long id;
#OneToMany(mappedBy = "job", fetch = FetchType.EAGER)
private Set<Worker> workers;
}
#Entity
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
#Table(name = "workers")
public class User extends DataAudit {
#Id
private Long id;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "job_id", referencedColumnName = "id")
private Job job;
}
Worker worker1 = workerService.getWorkerById(1);
Job job = new Job(1,worker1)
jobRepo.save(job)
The entities should be using bidirectional OneToMany association with #JoinTable:
#Entity
#Table(name = "job")
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
public class Job{
#Id
#Column(name = "id")
private Long id;
#OneToMany(fetch=FetchType.LAZY)
#JoinTable(name="job_workers",
joinColumns={#JoinColumn(name="job_id")},
inverseJoinColumns={#JoinColumn(name="worker_id")}
)
#Cascade(org.hibernate.annotations.CascadeType.ALL)
private Set<Worker> workers;
}
////////////////////////////////////////////////////////
#Entity
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
#Table(name = "workers")
public class User extends DataAudit {
#Id
private Long id;
#ManyToOne
#JoinTable(name="job_workers",
joinColumns={#JoinColumn(name="worker_id", insertable=false,updatable=false)},
inverseJoinColumns={#JoinColumn(name="job_id", insertable=false,updatable=false)})
private Job job;
}
Spring boot 2.2.2 ~ 2.2.4 ( the one i have tested so far)
I have an Abstract class
#MappedSuperclass
#Data
#EntityListeners(AuditingEntityListener.class)
#FilterDef(name = "tenantFilter", parameters = {#ParamDef(name = "tenantId", type = "int")})
#Filter(name = "tenantFilter", condition = "tenantId = :tenantId")
public abstract class BaseEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
....
}
A personnel class extending the abstract class
#EqualsAndHashCode(callSuper = true)
#Entity
#Table(name = "personnel")
#AllArgsConstructor
#NoArgsConstructor
#Data
#EntityListeners(AuditingEntityListener.class)
public class Personnel extends BaseEntity {
#NotNull
#OneToOne
#JoinColumn(name = "titleID")
private Title title;
// other fields and relationships of type ManyToOne, OneToMany
}
My Jpa Repository class
public interface PersonnelRepository extends JpaRepository<Personnel, Integer> {
Optional<PersonnelData> findByFileNo(String fileNo);
Optional<PersonnelData> findByIdAndDeletedOnIsNull(Integer id);
}
In my database, I have personnel records with some of the fields being null and the relationships.
In my controller when i do findById(1), it returns null but when I do findByIdAndDeletedOnIsNull(1), it returns the record.
I have tried #NotFound(action = NotFoundAction.IGNORE) on the relationships that are null with no success.