Unable to locate appropriate constructor on class JPA - java

I am getting the following error:
java.lang.IllegalArgumentException: org.hibernate.QueryException: unexpected char:
SELECT NEW com.classes.applicant.ApplicantEntry(app.indSsn, app.indivName, app.indAddrLocTx,app.indAddrCityNm,app.indAdrStateAb,app.indAddrZipCd, app.phoneNr,app.workPhoneNr) FROM TApplicant app WHERE app.indSsn = :ssn
The class Constructor is correct:
public ApplicantEntry(String indSsn, String indivName, String indAddrLocTx, String indAddrCityNm, String indAdrStateAb, String indAddrZipCd,
String phoneNr, String workPhoneNr) {
this.indSsn = indSsn;
this.indivName = indivName;
this.indAddrLocTx = indAddrLocTx;
this.indAddrCityNm = indAddrCityNm;
this.indAdrStateAb = indAdrStateAb;
this.indAddrZipCd = indAddrZipCd;
this.phoneNr = phoneNr;
this.workPhoneNr = workPhoneNr;
}
And the entity:
#Entity
#Table(name = "T_APPLICANT", schema = "APP")
public class TApplicant implements Serializable, Applicant {
private static final long serialVersionUID = 1L;
#Id
#Column(name = "IND_SSN", columnDefinition = "CHAR")
private String indSsn;
I do not understand why it is complaining about it is expecting all strings when it is recieving all strings.
I am running the query in intellij's persistence tool.
Full query in Repository:
#Query("SELECT NEW com.classes.applicant.ApplicantEntry(app.indSsn, app.indivName, "
+"app.indAddrLocTx,app.indAddrCityNm,app.indAdrStateAb,app.indAddrZipCd, app.phoneNr,app.workPhoneNr) "
+"FROM TApplicant app "
+"WHERE app.indSsn = :ssn ")
ApplicantEntry getApplicantEntry(#Param("ssn") String ssn);

Note for anyone using Lombok, the physical ordering of the fields in your class determines the order of your constructor parameters. The physical ordering of your class fields must match the ordering of the SELECT clause.
#AllArgsConstructor
public class Thing {
private String name;
private Date birthday;
}
// not the same as...
#AllArgsConstructor
public class Thing {
private Date birthday;
private String name;
}

ApplicantEntry constructor has 8 parameters while there are only 6 fields in the query.
The query doesn't look like correct jpa query. I think it should be
SELECT NEW org.classes.applicant.ApplicantEntry(
app.indSsn,
app.adnlPhysExamCd,
app.adnlPhysExamDt,
app.adultDepnQy,
app.adultDepnQy,
app.advRankRsnCd,
'placeholder',
'placeholder'
)
FROM ApplicantEntry app WHERE app.indSnn = :ssn

Related

Close projection doesn't give result for native query in spring boot

I am learning spring data close projection. However, it does not give the expected results. This means, instead of the expected interface, it gives org.springframework.aop.framework.JdkDynamicAopProxy. I have researched this for days and I couldn't find the solution. I have attached my code as well.
Entity class - Agent
#Entity
#Table(name = "TBLDMS_AGENT")
public class Agent {
#Id
#Column(name = "AGENT_ID")
private long agentId;
#Column(name = "AGENT_NAME")
private String agentName;
#Column(name = "ADDRESS")
private String address;
#Column(name = "CONTACT_NO")
private String contactNo;
#Column(name = "STATUS")
private boolean status;
#Column(name = "PROFILE_ID")
private int profileId;
Repository - AgentRepository
#Repository
public interface AgentRepository extends JpaRepository<Agent, Long> {
#Query(value =
"SELECT AGENT_NAME, " +
" PROFILE_CODE " +
"FROM TBLDMS_AGENT " +
"WHERE CONTACT_NO = :number", nativeQuery = true)
List<AgentInformation> findByContactNumber(#Param("number") String number);
public static interface AgentInformation {
String getAgentName();
String getProfileCode();
}
}
Usage
com.dms.agentmanagementservice.persister.AgentRepository.AgentInformation agent = agentRepository.findByContactNumber(number);
String agentName = agent.getAgentName();
String profileId = agent.getProfileCode();
But and getting null values for both agentName and profileId. Can someone tell me what I am doing wrong here?
Thank you very much in advance!

ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper [http-nio-8080-exec-3] (logExceptions:131) [] - The column name is not valid

trying to run a native query however I'm getting The column name sb_modelid is not valid. when attempting to map the return object to my model object in java? I have verified all the column names are correct.
1) Why is it referring to my column name as sb_modelid and not sbModelID?
2) Why is not being mapped to my POJO correctly?
Thanks!
Model Object:
package com.dish.wfm.linkingTickets.model.repository;
public class Model {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "sbModelID")
private Long sbModelId;
#Column(name = "modelID")
private String modelID;
#Column(name = "serialNumber")
private String serialNumber;
#Column(name = "serviceContractNumber")
private String serviceContractNumber;
}
Repo:
#Repository
public interface SBModelRepo extends JpaRepository<Model, Long> {
#Query(value = "select m.sbModelID, m.modelID, m.serialNumber, m.serviceContractNumber from sb.Model m where m.modelID = ?1 and m.serialNumber = ?2", nativeQuery = true)
Model findTopByModelIDAndSerialNumber(String modelID, String serialNumber);
}

java.lang.AbstractMethodError during createQuery

I am attempting to retrieve a list of results from my database, by following an example in this answer. Howeverm, I keep getting the following error:
java.lang.AbstractMethodError: org.hibernate.ejb.EntityManagerImpl.createQuery(Ljava/lang/String;Ljava/lang/Class;)Ljavax/persistence/TypedQuery;
Here is my code, to denote how I am calling this:
#Entity
#Table(name = "MY_TABLE")
public class CoolsEntity implements Serializable {
#Id
#Column(name = "ID", columnDefinition = "Decimal(10,0)")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;
#Column(name = "COOL_GUY_NAME")
private String name;
public String getId() {
return id;
}
public void setId(final String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name= name;
}
}
This code below generates the error:
final String sql = "select c from CoolsEntity c";
final TypedQuery<CoolsEntity> query = em.createQuery(sql, CoolsEntity.class);
final List<CoolsEntity> results = query.getResultList();
return results;
However, if I do something like this, I can see the results:
final String sql = "select c from CoolsEntity c";
final Query query = em.createQuery(sql);
#SuppressWarnings("unchecked")
final List<CoolsEntity> results = query.getResultList();
return results;
All of the references to em are imported through this package:
import javax.persistence.EntityManager
Shouldn't the two queries above generate the same result? Am I missing a cast to the List interface to allow this to work in the typed query?
You have an AbstractMethodError exception which is thrown when an application tries to call an abstract method.
You have quite a mix of Hibernate and JPA versions.
TypedQuery was introduced in JPA 2.0 and Hibernate implements this specification since 3.5.X
Suggesstion : Use implementation from Hibernate version 3.6.3 (or higher).
you are probably using two differnet version of interface EntityManager and implementation EntityManagerImpl.

How to define custom query using JPA #Query annotation

How do you use the JPA #Query notation? I wish to run the query:
SELECT region, winner_count
FROM awards_regions
WHERE award_id = ?
ORDER BY region
I have the repository:
public interface AwardsRegionsRepository extends JpaRepository<AwardRegion,AwardRegionPk>{
List<AwardRegion> findAllByOrderByAwardRegionPk_RegionAscSortKeyAsc();
List<AwardRegion> findByAwardRegionPk_RegionOrderBySortKeyAsc(String region);
#Query("select a.region, a.winner_count from awards_regions a "
+ "where a.award_id = :awardId order by a.region")
List<AwardRegion> findByAwardRegionPk_AwardId(#Param("awardId") Long awardId);
}
My entity Java beans are
#Entity
public class AwardRegion implements Serializable{
#EmbeddedId
private AwardRegionPk awardRegionPk;
#Column(name = "award")
private String award;
#Column(name = "sort_key")
private String sortKey;
#Column(name = "winner_count")
private String winnerCount;
}
...and embedded PK
#Embeddable
public class AwardRegionPk implements Serializable{
#Column(name = "region")
private String region;
#Column(name = "award_id")
private Long awardId;
}
It looks as though you are using the database table column names in your JPA query. Use the Java bean names.
public interface AwardsRegionsRepository extends JpaRepository<AwardRegion,AwardRegionPk>{
List<AwardRegion> findAllByOrderByAwardRegionPk_RegionAscSortKeyAsc();
List<AwardRegion> findByAwardRegionPk_RegionOrderBySortKeyAsc(String region);
#Query("select a.awardRegionPk.region, a.winnerCount from AwardRegion a "
+ "where a.awardRegionPk.awardId = :awardId order by a.awardRegionPk.region")
List<AwardRegion> findByAwardRegionPk_AwardId(#Param("awardId") Long awardId);
}

object db add compiste index doen't work

I try to add compostite Index in enttiy but not work
following error got :
[error] application - message= Unexpected database state: BTree 49 is not found, cause= [ObjectDB 2.5.4] javax.persistence.PersistenceException
Unexpected database state: BTree 49 is not found (error 147
Model Classes:
#Entity
#Table(name = Customer.TABLE_NAME)
#javax.jdo.annotations.Index(members=
{"addresses.firstName,addresses.lastName,addresses.company"})
public class Customer extends ObjectDBBaseModel<Customer> {
List<Address> addresses;
}
#Entity
#Table(name = Address.TABLE_NAME)
public class Address<T extends ObjectDBBaseModel> extends ObjectDBBaseModel<T> {
public static final String TABLE_NAME = "address";
public static final Address NULL = new Address();
public static ODBFinder<Address> find = new ODBFinder<>(Address.class, NULL);
public Address() {
super((Class<T>) Address.class);
}
#Column(name = "address_type_enum")
#Enumerated(EnumType.STRING)
private AddressTypeEnum addressTypeEnum;
#Column(name = "gender_enum")
#Enumerated(EnumType.STRING)
private GenderEnum genderEnum;
private String title;
private String firstName;
private String lastName;
private String company;
private String street1;
}
The index definition is invalid, since a multi path index is limited to one entity class (and additional embeddable classes) but cannot spread over multiple entity classes.
In this case you should use two separate indexes:
Simple index on the collection of addresses.
Composite index on the three fields in Address.
ObjectDB will maintain each index separately but will join them together in relevant queries.

Categories

Resources