Keycloack does not detect #SqlResultSetMapping in custom provider jar - keycloak

I am trying to create a custom query in Keycloak in my custom provider Jar. The annotation below is not detected. I had to resort to use the call that returns a list of objects from which I can extract the columns from the result set. However I would like to use the SqlResultSetMapping interface. Here is the code that I am using.
#MappedSuperclass
#SqlResultSetMapping( name = "GroupTabularDataMapping",
classes = {
#ConstructorResult(
targetClass = GroupTabularData.class,
columns = {
#ColumnResult(name = "id"),
#ColumnResult(name = "name"),
#ColumnResult(name = "updated_by"),
#ColumnResult(name = "updated_by_email"),
#ColumnResult(name = "last_updated"),
#ColumnResult(name = "count",type = Long.class)
})
})
public class GroupTabularData {
private String id;
private String name;
private String updatedBy;
private String updatedByEmail;
private String lastUpdated;
private BigInteger count;
public GroupTabularData(String id, String name, String updatedBy, String updatedByEmail,
String lastUpdated, BigInteger count) {
this.id = id;
this.name = name;
this.updatedBy = updatedBy;
this.updatedByEmail = updatedByEmail;
this.lastUpdated = lastUpdated;
this.count = count;
}
public GroupTabularData(Object objArr) {
Object[] arr = (Object[]) objArr;
this.id = arr[0].toString();
this.name = arr[1].toString();
this.updatedBy = arr[2].toString();
this.updatedByEmail = arr[3].toString();
this.lastUpdated = arr[4].toString();
this.count = (BigInteger)arr[5];
}
}
This call works:
Object resultData = q.getResultList().stream().map(
objArr -> new GroupTabularData(objArr)).collect(Collectors.toList());
But this one does not:
Query q = em.createNativeQuery(GROUP_TABULAR_QUERY, "GroupTabularDataMapping");
List<GroupTabularData> resultData = q.getResultList();
I imagine that the reason is because I need to add my class to the scanning list of JPA but I don't know which file I need to update or if it is some other configuration. I appreciate any leads.

Related

How to query a many to many entity list via Specification API in Spring Data JPA

I have 2 entities with Many-To-Many relationships
public class Enterprise{
#Id
#Column(name = "id", nullable = false, length = 50)
#GeneratedValue(generator = "jpa-uuid")
private String id;
fields...
#ManyToMany
#JoinTable(name = "enterprise_to_tag",
joinColumns = #JoinColumn(name = "enterprise"),
inverseJoinColumns = #JoinColumn(name = "tag"))
private Set<EnterpriseTag> tags;
}
and
public class EnterpriseTag{
#Id
#Column(name = "id", nullable = false, length = 50)
#GeneratedValue(generator = "jpa-uuid")
private String id;
fields...
#ManyToMany(mappedBy = "tags")
private Set<Enterprise> enterprises;
}
I want to query enterprise list by some tags' ID then pack them to Page
private Page<Enterprise> searchEnterprise(int number, int size, String keyword, String tags, String county)
throws BusinessException {
validPageNumberAndPageSize(number, size);
Pageable pageable = PageRequest.of(number, size);
Specification<Enterprise> specification = (Specification<Enterprise>) (root, criteriaQuery, criteriaBuilder) -> {
criteriaQuery.distinct(true);
List<Predicate> predicates = new ArrayList<>();
if (StringUtils.isNoneBlank(keyword)) {
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + keyword + "%");
Predicate predicateSerialNumber = criteriaBuilder.like(root.get("serialNumber"), "%" + keyword + "%");
predicates.add(criteriaBuilder.and(criteriaBuilder.or(predicateName, predicateSerialNumber, predicateOrganizationCode)));
}
return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction();
};
//filter by tags here
if (StringUtils.isNoneBlank(tags)) {
List<String> tagIds = Arrays.asList(StringUtils.split(tags, ','));
List<Enterprise> enterprises = enterpriseRepository.findAll(specification).stream().filter(enterprise ->
enterprise.getTags().stream().map(EnterpriseTag::getId).collect(Collectors.toList()).containsAll(tagIds))
.collect(Collectors.toList());
return new PageImpl<>(enterprises, pageable, enterprises.size());
} else {
return enterpriseRepository.findAll(specification, pageable);
}
}
I don't know how to write this query. I have to handle it base on a database query result. But it's risky. If too much data is queried from the database, it will take up a lot of memory. Please help me to write this query by Specification API. Thanks.

How to use a #ConstructorResult with a Set<SomeEnum> field

I'm trying to create a #NamedNativeQuery with a #ConstructorResult for a class that has a field with a Set of enum values.
VeterinarianJPA.java:
#Entity
#Table(name = "veterinarians")
#Setter
#Getter
#NoArgsConstructor
#NamedNativeQueries({
#NamedNativeQuery(
name = VeterinarianJPA.FIND_ALL_VETS,
query = "SELECT v.id, v.name, vs.specialisations " +
"FROM veterinarians v " +
"JOIN veterinarian_specialisations vs ON v.id = vs.vet_id",
resultSetMapping = VeterinarianJPA.VETERINARIAN_RESULT_MAPPER
)})
#SqlResultSetMappings({
#SqlResultSetMapping(
name = VeterinarianJPA.VETERINARIAN_RESULT_MAPPER,
classes = #ConstructorResult(
targetClass = Veterinarian.class,
columns = {
#ColumnResult(name = "id", type = Long.class),
#ColumnResult(name = "name"),
#ColumnResult(name = "specialisations", type = Set.class)
}
)
)})
class VeterinarianJPA {
static final String FIND_ALL_VETS = "net.kemitix.naolo.gateway.data.jpa.findAllVets";
static final String VETERINARIAN_RESULT_MAPPER = "net.kemitix.naolo.gateway.data.jpa.Veterinarian";
#Id
#GeneratedValue
private Long id;
private String name;
#ElementCollection
#Enumerated(EnumType.STRING)
#CollectionTable(
name = "veterinarian_specialisations",
joinColumns = #JoinColumn(name = "vet_id")
)
private final Set<VetSpecialisation> specialisations = new HashSet<>();
}
Veterinarian.java:
public final class Veterinarian {
private Long id;
private String name;
private Set<VetSpecialisation> specialisations;
public Veterinarian() {
}
public Veterinarian(final long id,
final String name,
final Set<VetSpecialisation> specialisations) {
this.id = id;
this.name = name;
this.specialisations = new HashSet<>(specialisations);
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public Set<VetSpecialisation> getSpecialisations() {
return new HashSet<>(specialisations);
}
}
VetSpecialisation.java:
public enum VetSpecialisation {
RADIOLOGY,
DENTISTRY,
SURGERY
}
When I attempt to execute the named query:
entityManager.createNamedQuery(VeterinarianJPA.FIND_ALL_VETS, Veterinarian.class)
.getResultStream()
I get the following exception:
java.lang.IllegalArgumentException: Could not locate appropriate constructor on class : net.kemitix.naolo.entities.Veterinarian
at org.hibernate.loader.custom.ConstructorResultColumnProcessor.resolveConstructor(ConstructorResultColumnProcessor.java:92)
at org.hibernate.loader.custom.ConstructorResultColumnProcessor.performDiscovery(ConstructorResultColumnProcessor.java:45)
at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:494)
at org.hibernate.loader.Loader.processResultSet(Loader.java:2213)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2169)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1930)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1892)
at org.hibernate.loader.Loader.scroll(Loader.java:2765)
at org.hibernate.loader.custom.CustomLoader.scroll(CustomLoader.java:383)
at org.hibernate.internal.SessionImpl.scrollCustomQuery(SessionImpl.java:2198)
at org.hibernate.internal.AbstractSharedSessionContract.scroll(AbstractSharedSessionContract.java:1058)
at org.hibernate.query.internal.NativeQueryImpl.doScroll(NativeQueryImpl.java:217)
at org.hibernate.query.internal.AbstractProducedQuery.scroll(AbstractProducedQuery.java:1462)
at org.hibernate.query.internal.AbstractProducedQuery.stream(AbstractProducedQuery.java:1486)
at org.hibernate.query.Query.getResultStream(Query.java:1110)
I expect that the SQL is returning multiple rows for a multi-valued Set rather than a single value, which is causing the constructor not to match. How do I change the SQL to produce the correct input to the constructor, or is there another configuration change I need to make?
Well, I'm not sure if that's even possible in the way you want to to this. But you can use LISTAGG function on specialisations table to inline the specialisations with veterinarians by using some kind of separator.
So the query should look like this:
SELECT v.id, v.name
(SELECT LISTAGG(vs.type, ';')
WITHIN GROUP (ORDER BY vs.type)
FROM veterinarian_specialisations vs
WHERE vs.vet_id = v.id) specialisations
FROM veterinarians v;
The query will return veterinarian and his semicolon separated specialisations:
1 NAME DENTISTRY;RADIOLOGY
And then in your Veterinarian class constructor you must remap String result back to Set of VetSpecialisation. I used Java 8 stream api just for convenience.
public final class Veterinarian {
private Long id;
private String name;
private Set<VetSpecialisation> specialisations;
public Veterinarian() {
}
public Veterinarian(final long id,
final String name,
final String specialisations) {
this.id = id;
this.name = name;
this.specialisations = Arrays.asList(specialisations.split(";"))
.stream()
.map(VetSpecialisation::valueOf) //Map string to VetSpecialisation enum.
.collect(Collectors.toSet());
}

Could not write content: failed to lazily initialize a collection of role, could not initialize proxy - no Session through reference chain

I am getting this error.I don't know what is this error and how to solve it.
Error:-
Could not write content: failed to lazily initialize a collection of role: com.neostencil.modules.testmanagement.model.entities.question.Question.options, could not initialize proxy - no Session (through reference chain: com.neostencil.modules.testmanagement.responses.question.FetchQuestionById[\"question\"]->com.neostencil.modules.testmanagement.model.entities.question.Question[\"options\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.neostencil.modules.testmanagement.model.entities.question.Question.options, could not initialize proxy - no Session (through reference chain: com.neostencil.modules.testmanagement.responses.question.FetchQuestionById[\"question\"]->com.neostencil.modules.testmanagement.model.entities.question.Question[\"options\"])"
I am using #ElementCollection to store List of string.This is my entity model
#Entity
#Table(name = "Question")
public class Question extends DomainObject {
#GeneratedValue(strategy = GenerationType.TABLE, generator = "question_gen")
#TableGenerator(name = "question_gen", table = "ns_question_id_gen", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL", pkColumnValue = "QuestionId_Gen", initialValue = 10000, allocationSize = 100)
#Id
#Column(name = "question_id")
private int questionId;
#Column(name = "question_text")
private String text;
#Column(name = "question_type")
#Enumerated(EnumType.STRING)
private QuestionType type;
#Column(name = "question_timeout")
private Double timeout;
#Column(name = "marks")
private Double marks;
#Column(name = "negative_mark")
private Double negativeMark;
#Column(name = "question_position")
private Double position;
#ElementCollection()
#CollectionTable(name = "ns_question_option_items",joinColumns = #JoinColumn(name = "question_id"))
#Column(name = "question_options")
public List<String> options = new ArrayList<>();
public List<String> getOptions() {
return options;
}
public void setOptions(List<String> options) {
this.options = options;
}
#Column(name = "subject")
private String subject;
#Column(name = "test_id")
private BigInteger testId;
public BigInteger getTestId() {
return testId;
}
public void setTestId(BigInteger testId) {
this.testId = testId;
}
public Question() {
super();
}
public Question(int questionId, String questionText, QuestionType questionType, Double questionTimeout, Double marks, Double negativeMark, Double questionPosition, List<String> questionOptions, String subject, Answer answer) {
super();
this.questionId = questionId;
this.text = questionText;
this.type = questionType;
this.timeout = questionTimeout;
this.marks = marks;
this.negativeMark = negativeMark;
this.position = questionPosition;
this.options = questionOptions;
this.subject = subject;
}
public Question(String createdBy, Timestamp creationDate, int version, Timestamp lastModifiedDate,
String lastModifiedBy, RecordStatus recordStatus) {
super(createdBy, creationDate, version, lastModifiedDate, lastModifiedBy, recordStatus);
}
public int getQuestionId() {
return questionId;
}
public void setQuestionId(int questionId) {
this.questionId = questionId;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public QuestionType getType() {
return type;
}
public void setType(QuestionType type) {
this.type = type;
}
public Double getTimeout() {
return timeout;
}
public void setTimeout(Double timeout) {
this.timeout = timeout;
}
public Double getMarks() {
return marks;
}
public void setMarks(Double marks) {
this.marks = marks;
}
public Double getNegativeMark() {
return negativeMark;
}
public void setNegativeMark(Double negativeMark) {
this.negativeMark = negativeMark;
}
public Double getPosition() {
return position;
}
public void setPosition(Double position) {
this.position = position;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}
I am getting this error while fetching Question model from a table using question id.This is my function where I am fetching Question
public Question fetchQuestionById(int id) {
SessionFactory sessionFactory = createSessionFactory(configuration);
Session session = sessionFactory.openSession();
Question question = session.get(Question.class, new Integer(id));
List<String> options = question.getOptions();
question.setOptions(options);
session.close();
return question;
}
I am using Postgres database. I want to store a list of string in the database so that I am using #ElementCollection annotation for that. I am searched on the internet but found nothing. What is the problem here?Is the correct approach to store list of string in the database? If not, please tell me and what is the solution of above probelm.

How to query for entities by their collection value

I'm using jpa and I have the following entity:
#Entity
#Table(name="favorites_folders")
public class FavoritesFolder {
private static final long serialVersionUID = 1L;
#Id
private String id;
#NotNull
#Size(min = 1, max = 50)
public String name;
#ElementCollection(fetch = FetchType.LAZY)
#CollectionTable(
name="favorites_products",
joinColumns=#JoinColumn(name="folder_id")
)
#Column(name="product_id")
#NotNull
private Set<String> productsIds = new HashSet<String>();
}
What I want to do is to get a set of FavoritesFolder entities that contains the string "favorite-id" in their productsIds member set.
Does anyone know how can it be done in criteria api?
Update:
I'm thinking the following sql should do the trick but I'm not sure how to do it in either JPQL or Criteria API:
select * from favorites_folders join favorites_products on favorites_folders.id = favorites_products.folder_id where favorites_products.product_id = 'favorite-id'
To get a set of FavoritesFolder entities that contains the string "favorite-id" in their productsIds member set using criteria api you should do the following:
CriteriaBuilder cb = em.getCriteriaBuilder(); //em is EntityManager
CriteriaQuery<FavoritesFolder> cq = cb.createQuery(FavoritesFolder.class);
Root<FavoritesFolder> root = cq.from(FavoritesFolder.class);
Expression<Collection<String>> productIds = root.get("productsIds");
Predicate containsFavoritedProduct = cb.isMember("favorite-id", productIds);
cq.where(containsFavoritedProduct);
List<FavoritesFolder> favoritesFolders = em.createQuery(cq).getResultList();
More information on Collections in JPQL and Criteria Queries.
Just another way using IN
#Entity
public class UserCategory implements Serializable {
private static final long serialVersionUID = 8261676013650495854L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#ElementCollection
private List<String> categoryName;
(...)
}
Then you can write a Criteria query like
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<UserCategory> q = cb.createQuery(UserCategory.class);
Root<UserCategory> root = q.from(UserCategory.class);
Predicate predicate = cb.conjunction();
Predicate p1 = cb.equal(root.get(UserCategory_.targetSiteType), siteType.getName());
Predicate p2 = root.get(UserCategory_.categoryName).in(category);
predicate = cb.and(p1,p2);
q.where(predicate);
TypedQuery<UserCategory> tq = entityManager.createQuery(q);
List<UserCategory> all = tq.getResultList();
if (all == null || all.size() == 0){
return null;
}else if (all.size() > 1){
throw new Exception("Unexpected result - "+all.size());
}else{
return all.get(0);
}
This is my work around that works.
I'm using Springboot 1.5.9. I don't have time to identify the root cause. What I know is such nested property been ignored when get through JacksonMappingAwareSortTranslator.
So what I did to workaround this is not to use Sort object created by resolvers.
Here's my code in Kotlin. Without doing this, the pageable.sort is null and sorting does not work. And my code will create a new PageRequest object that has non-null sort that works.
#RequestMapping("/searchAds", method = arrayOf(RequestMethod.POST))
fun searchAds(
#RequestBody cmd: AdsSearchCommand,
pageable: Pageable,
resourceAssembler: PersistentEntityResourceAssembler,
sort: String? = null
): ResponseEntity<PagedResources<Resource<Ads>>> {
val page = adsService.searchAds(cmd, pageable.repairSortIfNeeded(sort))
resourceAssembler as ResourceAssembler<Ads, Resource<Ads>>
return adsPagedResourcesAssembler.toResource(page, resourceAssembler).toResponseEntity()
}
fun Pageable.repairSortIfNeeded(sort: String?): Pageable {
return if (sort.isNullOrEmpty() || this.sort != null) {
this
} else {
sort as String
val sa = sort.split(",")
val direction = if (sa.size > 1) Sort.Direction.valueOf(sa[1]) else Sort.Direction.ASC
val property = sa[0]
PageRequest(this.pageNumber, this.pageSize, direction, property)
}
}

how to start from "0" an UNSIGNED AUTO_INCREMENT field?

I have the following tables :
wherein idclient is unsigned auto_increment.
code of the Client entity:
import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
#Entity
#Table(name = "CLIENT", catalog = "TEST", schema = "PUBLIC")
#XmlRootElement
#NamedQueries({
#NamedQuery(name = "Client.findAll", query = "SELECT c FROM Client c"),
#NamedQuery(name = "Client.findByIdclient", query = "SELECT c FROM Client c WHERE c.idclient = :idclient"),
#NamedQuery(name = "Client.findByLibel", query = "SELECT c FROM Client c WHERE c.libel = :libel"),
#NamedQuery(name = "Client.findByAdresse", query = "SELECT c FROM Client c WHERE c.adresse = :adresse"),
#NamedQuery(name = "Client.findByNomResp", query = "SELECT c FROM Client c WHERE c.nomResp = :nomResp"),
#NamedQuery(name = "Client.findByTelPortable", query = "SELECT c FROM Client c WHERE c.telPortable = :telPortable"),
#NamedQuery(name = "Client.findByTelFixe", query = "SELECT c FROM Client c WHERE c.telFixe = :telFixe"),
#NamedQuery(name = "Client.findByFax", query = "SELECT c FROM Client c WHERE c.fax = :fax"),
#NamedQuery(name = "Client.findByCodeTva", query = "SELECT c FROM Client c WHERE c.codeTva = :codeTva"),
#NamedQuery(name = "Client.findByCodeExo", query = "SELECT c FROM Client c WHERE c.codeExo = :codeExo"),
#NamedQuery(name = "Client.findByBanque", query = "SELECT c FROM Client c WHERE c.banque = :banque"),
#NamedQuery(name = "Client.findByRib", query = "SELECT c FROM Client c WHERE c.rib = :rib"),
#NamedQuery(name = "Client.findByCredit", query = "SELECT c FROM Client c WHERE c.credit = :credit"),
#NamedQuery(name = "Client.findByEchance", query = "SELECT c FROM Client c WHERE c.echance = :echance"),
#NamedQuery(name = "Client.findByMail", query = "SELECT c FROM Client c WHERE c.mail = :mail"),
#NamedQuery(name = "Client.findByEtat", query = "SELECT c FROM Client c WHERE c.etat = :etat")})
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "IDCLIENT", nullable = false)
private Integer idclient;
#Basic(optional = false)
#Column(name = "LIBEL", nullable = false, length = 100)
private String libel;
#Basic(optional = false)
#Column(name = "ADRESSE", nullable = false, length = 100)
private String adresse;
#Basic(optional = false)
#Column(name = "NOM_RESP", nullable = false, length = 60)
private String nomResp;
#Basic(optional = false)
#Column(name = "TEL_PORTABLE", nullable = false, length = 16)
private String telPortable;
#Basic(optional = false)
#Column(name = "TEL_FIXE", nullable = false, length = 16)
private String telFixe;
#Basic(optional = false)
#Column(name = "FAX", nullable = false, length = 16)
private String fax;
#Basic(optional = false)
#Column(name = "CODE_TVA", nullable = false, length = 30)
private String codeTva;
#Basic(optional = false)
#Column(name = "CODE_EXO", nullable = false, length = 30)
private String codeExo;
#Basic(optional = false)
#Column(name = "BANQUE", nullable = false, length = 60)
private String banque;
#Basic(optional = false)
#Column(name = "RIB", nullable = false, length = 22)
private String rib;
#Basic(optional = false)
#Column(name = "CREDIT", nullable = false)
private double credit;
#Basic(optional = false)
#Column(name = "ECHANCE", nullable = false)
private int echance;
#Basic(optional = false)
#Column(name = "MAIL", nullable = false, length = 70)
private String mail;
#Basic(optional = false)
#Column(name = "ETAT", nullable = false)
private char etat;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "clientIdclient")
private List<Facture> factureList;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "clientIdclient")
private List<FactProforma> factProformaList;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "clientIdclient")
private List<Bl> blList;
public Client() {
}
public Client(Integer idclient) {
this.idclient = idclient;
}
public Client(Integer idclient, String libel, String adresse, String nomResp, String telPortable, String telFixe, String fax, String codeTva, String codeExo, String banque, String rib, double credit, int echance, String mail, char etat) {
this.idclient = idclient;
this.libel = libel;
this.adresse = adresse;
this.nomResp = nomResp;
this.telPortable = telPortable;
this.telFixe = telFixe;
this.fax = fax;
this.codeTva = codeTva;
this.codeExo = codeExo;
this.banque = banque;
this.rib = rib;
this.credit = credit;
this.echance = echance;
this.mail = mail;
this.etat = etat;
}
public Integer getIdclient() {
return idclient;
}
public void setIdclient(Integer idclient) {
this.idclient = idclient;
}
public String getLibel() {
return libel;
}
public void setLibel(String libel) {
this.libel = libel;
}
public String getAdresse() {
return adresse;
}
public void setAdresse(String adresse) {
this.adresse = adresse;
}
public String getNomResp() {
return nomResp;
}
public void setNomResp(String nomResp) {
this.nomResp = nomResp;
}
public String getTelPortable() {
return telPortable;
}
public void setTelPortable(String telPortable) {
this.telPortable = telPortable;
}
public String getTelFixe() {
return telFixe;
}
public void setTelFixe(String telFixe) {
this.telFixe = telFixe;
}
public String getFax() {
return fax;
}
public void setFax(String fax) {
this.fax = fax;
}
public String getCodeTva() {
return codeTva;
}
public void setCodeTva(String codeTva) {
this.codeTva = codeTva;
}
public String getCodeExo() {
return codeExo;
}
public void setCodeExo(String codeExo) {
this.codeExo = codeExo;
}
public String getBanque() {
return banque;
}
public void setBanque(String banque) {
this.banque = banque;
}
public String getRib() {
return rib;
}
public void setRib(String rib) {
this.rib = rib;
}
public double getCredit() {
return credit;
}
public void setCredit(double credit) {
this.credit = credit;
}
public int getEchance() {
return echance;
}
public void setEchance(int echance) {
this.echance = echance;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public char getEtat() {
return etat;
}
public void setEtat(char etat) {
this.etat = etat;
}
#XmlTransient
public List<Facture> getFactureList() {
return factureList;
}
public void setFactureList(List<Facture> factureList) {
this.factureList = factureList;
}
#XmlTransient
public List<FactProforma> getFactProformaList() {
return factProformaList;
}
public void setFactProformaList(List<FactProforma> factProformaList) {
this.factProformaList = factProformaList;
}
#XmlTransient
public List<Bl> getBlList() {
return blList;
}
public void setBlList(List<Bl> blList) {
this.blList = blList;
}
#Override
public int hashCode() {
int hash = 0;
hash += (idclient != null ? idclient.hashCode() : 0);
return hash;
}
#Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Client)) {
return false;
}
Client other = (Client) object;
if ((this.idclient == null && other.idclient != null) || (this.idclient != null && !this.idclient.equals(other.idclient))) {
return false;
}
return true;
}
#Override
public String toString() {
return "glob.entitys.Client[ idclient=" + idclient + " ]";
}
}
when I try to insert a row into the data base :
Utilisateur user=new Utilisateur(loginActuel);
Client client=new Client(0);// the error comes from here
Facture fact=new Facture(null,new Date());
fact.setClientIdclient(client);
fact.setUtilisateurLogin(user);
FactureJpaController fjc=new FactureJpaController(emf);
fjc.create(fact);
I get this ugly error(but when i set new Client(1) it works well):
Exception in thread "AWT-EventQueue-0" javax.persistence.RollbackException: Exception [EclipseLink-7197] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Null or zero primary key encountered in unit of work clone [glob.entitys.Client[ idclient=0 ]], primary key [0]. Set descriptors IdValidation or the "eclipselink.id-validation" property.
how to solve this problem ?
remark: the client idclient = 0 is already inserted in the Database(but manually)
I'd like once and for all overcome this "problem" , how to prevent JPA or H2 Database to start from 0 ?
H2 does allow to use 0 as the primary key. The error message doesn't come from H2.
However, it seems to me that some (older?) version of EclipseLink doesn't allow to use 0.
the client idclient = 0 is already inserted in the Database
It seems this is not supported by this version of EclipseLink. It looks like to work around this problem, you should not use the value 0.
There are two ways how to allow zeroes in primary keys in Eclipselink:
Parameter in persistence.xml:
<property name="eclipselink.id-validation" value="NULL"/>
PrimaryKey annotation on concerned entity class:
#PrimaryKey(validation = IdValidation.NULL)
For JPA (specification 2.0) having (or negative) value for id is fine. And also as primary key value for H2.
Older versions of EclipseLink do consider value 0 or smaller as invalid primary key. See for example following:Bug 249948. So updating EclipseLink can help.
By the way, why you do set in constructor value for idclient that is supposed to be generated?
I had this error and adding the following annotations to my jpa identity resolved it:
#Column(name = "ID_SEARCH_LOG", nullable = false, insertable = true, updatable = true, length = 10, precision = 0)
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int idSearchLog;
The docs say
By default, EclipseLink interprets zero as null for primitive types that cannot be null (such as int and long) causing zero to be an invalid value for primary keys.
but also that that it is possible to change this behaviour in either the persistence.xml or on a particular entity.
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Entities/Ids/Id#Allowing_Zero_Value_Primary_Keys