Spring Data JPA Mapping Exception No Dialect mapping for JDBC type: -9 - spring

I am trying to use a projection and am getting the following error. Not sure what the issue is.
Here is the projection:
public interface UserMini {
Long getApproverKey();
String getEmailAddress();
String getFirstName();
String getLastName();
Long getUserKey();
String getUserName();
}
Here is the Query in the repository:
#RestResource(path="getUserMini")
#Query(value="SELECT approverKey, emailAddress, firstName, lastName, userKey, userName FROM [dbo].BdmUser WHERE userKey = :userKey ", nativeQuery=true)
UserMini getUserMini(#Param("userKey") long userKey);
Here is the Entity
#Table (name="[BdmUser]")
#Entity
public class BdmUser {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long userKey;
private Long priceListKey;
private String firstName;
private String lastName;
private String userName;
private String emailAddress;
private String password;
private Boolean active;
private Long approverKey;
private BigDecimal orderLimit;
private Long salesOfficeKey;
private Long reportsToId;
private Boolean requiresOrderApproval;
private Date lastLoginDate;
private String rowIsCurrent;
private Date rowStartDate;
private Date rowEndDate;
#Column(name="HashByteValueType1", updatable=false, insertable=false)
private String hashByteValueType1;
#Column(name="HashByteValueType2", updatable=false, insertable=false)
private String hashByteValueType2;
private String rowChangeReason;
#Column(name="DQScoreKey")
private Integer dqScoreKey;
private Integer insertAuditKey;
private Integer updateAuditKey;

Try to cast the NVARCHAR to VARCHAR in your query
CONVERT(varchar,theNVarcharColumn)

Related

Hibernate Enver - Listening for only one change in referenced class

I have an entity as shown below that I am auditing using Hibernate Enver
#Entity
#Table(name = "watch_item")
#Audited
public class WatchItemEntity implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Type(type = "uuid-char")
#Column(name = "watch_item_id", columnDefinition = "VARCHAR(36)")
private UUID watchItemId;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "watch_model_id")
private WatchModelEntity watchModel;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "private_user_id")
private PrivateUserEntity privateUser;
private String serialNumber;
private Integer productionYear;
private String generalCondition;
private Boolean isMovementFullyFunctional;
private Boolean isInOriginalCondition;
private String comment;
private Boolean isProofOfPurchaseAvailable;
private String country;
private Boolean isCustomsDeclared;
private Boolean hasPaper;
private Boolean hasBox;
private String otherAccessories;
#CreatedDate private LocalDateTime createdDate;
#LastModifiedDate private LocalDateTime modifiedDate;
private String lastServiceProvider;
private LocalDate lastServiceDate;
private BigDecimal lastServiceCost;
private BigDecimal purchasedPrice;
private LocalDate purchasedOn;
...
}
As you can see, it has a PrivateUserEntity field. I want Hibernate Envers to record a change when the privateUser changes (and not record changes in PrivateUserEntity that correspond to the privateUser). However, I don't want to create a Private_User_Aud table. To give some context, a WatchItem can only be owned by one PrivateUser and hence, when the PrivateUser field changes, that means that the WatchItem's owner changed. The entity can be seen below
#Entity
#Table(name = "private_user")
public class PrivateUserEntity implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Type(type = "uuid-char")
#Column(name = "private_user_id", columnDefinition = "VARCHAR(36)")
private UUID privateUserId;
#OneToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "user_id")
private UserEntity user;
#LastModifiedDate private LocalDate modifiedDate;
private String title;
private String firstName;
private String lastName;
private String phone;
private LocalDate dateOfBirth;
private String email;
private String gender;
private String nationality;
private String residencyPermitType;
private LocalDate residencyPermitValidSince;
private String preferredLanguage;
...
}
Is this possible? And if so, how?
You can disable audit for it, doesn't work?
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "private_user_id")
#NotAudited
private PrivateUserEntity privateUser;

Hibernate : Unable to locate appropriate constructor on class

i want to retrieve data with DTO Projection using sprind data jpa, but unfortunately when i call the method an error has occurred :
[2020-05-28 21:02:03] Unable to locate appropriate constructor on class [com.burgerbuilder.backend.DTO.Response.UserResponse]. Expected arguments are: java.util.UUID, java.lang.String , java.lang.String , java.lang.String , java.util.Collection
[select new com.burgerbuilder.backend.DTO.Response.UserResponse(u.id,u.email,u.name,u.lastName,u.authorities) from com.burgerbuilder.backend.Model.User u where u.id=:id]
my repository :
#Repository
public interface UserRepository extends JpaRepository<User, UUID> {
#Query("select new com.burgerbuilder.backend.DTO.Response.UserResponse(u.id,u.email,u.name,u.lastName,u.authorities) from User u where u.id=:id")
Optional<UserResponse> findUserById(#Param("id") String id);}
User class:
#Entity
public class User implements UserDetails {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Type(type=”uuid-char”)
private UUID id;
#NotNull
private String email;
#NotNull
private String password;
private String name;
private String lastName;
private String phoneNumber;
private String emailVerificationToken;
private boolean isEmailVerified=false;
private boolean isPhoneNumberVerified=false;
#OneToMany(mappedBy = “user”,cascade = CascadeType.ALL)
private List<Authority> authorities=new ArrayList();
DTO Class :
public class UserResponse {
private String userId;
private String email;
private String name;
private String lastName;
private List<Authority> authorities=new ArrayList<>();
public UserResponse(UUID userId, String email, String name, String lastName, List<Authority> authorities) {
this.userId = userId.toString();
this.email = email;
this.name = name;
this.lastName = lastName;
this.authorities=authorities;
}
}
can someone help me please ?

Spring JPA Update Entity

I'm trying to update my user entity and I have an error that comes to mind:
ERROR: A NULL value violates the NOT NULL constraint of the "id" column Detail: The failed row contains (null, 1, 1)
The problem surely stems from my relationship between user and profile which is n-n
public class Utilisateur implements Serializable {
private static final long serialVersionUID = 1L;
#Id
private Integer id;
private Integer fixe;
private Boolean deleted;
private Boolean actif;
private String email;
private Integer mobile;
private String motDePasse;
private String nom;
private String prenom;
#ManyToMany
private List<Profil> profils = new ArrayList<Profil>();
public Utilisateur() {
}
}
public class Profil implements Serializable {
private static final long serialVersionUID = 1L;
#Id
private Integer id;
private String codeProfil;
private String libelleProfil;
#JsonManagedReference
#ManyToMany
private List<MenuAction> menuActions = new ArrayList<MenuAction>();
public Profil() {
}
}
How you generate value for your id?
Seems you need some way to generate value for you ID.
For example, use #GeneratedValue, like:
#GeneratedValue(strategy = IDENTITY)

Using Spring JPA Query to populate a pojo

I am using JPA and Hibernate to carry out a simple query. It is a join pulling from two tables, an application table, and a Transaction table. I can see data coming back but it is not able to populate this POJO I created, and it is failing to make the conversion. This is the query:
public interface TransactionRepository extends JpaRepository<Transaction, String>{
#Query(value="SELECT "
+ " TXN.TAX_ID taxId, "
+ " APP.AP_DT apDt, "
+ " TXN.TXN_ID txnId"
+ " FROM "
+ " APPLICATION APP "
+ " JOIN "
+ " TRANSACTION TXN "
+ " ON "
+ " TXN.TXN_ID = APP.TXN_ID "
+ " WHERE"
+ " APP.CONFIRMATION_ID = ?1", nativeQuery=true)
AnnuityNetPdfToFims getInfoForPdfToFims(String confirmationId);
Here is the Transaction Entity
#Entity
#Table(name="\"TRANSACTION\"")
#NamedQuery(name="Transaction.findAll", query="SELECT t FROM Transaction t")
public class Transaction implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name="TXN_ID")
private String txnId;
#Column(name="ACCT_NUM")
private String acctNum;
#Column(name="ACCT_TYPE_CODE")
private String acctTypeCode;
#Column(name="AGENT_ID")
private String agentId;
#Column(name="AUTO_SUITABILITY_IND")
private String autoSuitabilityInd;
#Column(name="CARRIER_ID")
private String carrierId;
#Column(name="CHANNEL_NAME")
private String channelName;
#Column(name="COMPANY_NAME")
private String companyName;
#Column(name="DELIVERY_STATE")
private String deliveryState;
#Column(name="FIMS_DOCUMENT_ID")
private String fimsDocumentId;
#Column(name="INTERNAL_CONVERSION_IND")
private String internalConversionInd;
#Column(name="JOINT_OWNER_STATE")
private String jointOwnerState;
#Column(name="OWNER_FIRST_NAME")
private String ownerFirstName;
#Column(name="OWNER_LAST_NAME")
private String ownerLastName;
#Column(name="OWNER_TYPE_CODE")
private String ownerTypeCode;
#Column(name="PERSON_IND")
private String personInd;
#Column(name="PRODUCT_CODE")
private String productCode;
#Column(name="PRODUCT_CUSIP")
private String productCusip;
#Column(name="RESIDENCE_STATE")
private String residenceState;
#Column(name="REVIEWER_ID")
private String reviewerId;
#Column(name="SOLICITATION_STATE")
private String solicitationState;
#Column(name="STATUS_CODE")
private String statusCode;
#Column(name="STEP_ACTION_CODE")
private String stepActionCode;
#Column(name="STEP_NAME")
private String stepName;
#Column(name="TAX_ID")
private String taxId;
#Column(name="TR_BY_ID")
private String trById;
#Column(name="TR_TS")
private Timestamp trTs;
#Column(name="TXN_QUALIFIED_IND")
private String txnQualifiedInd;
#Column(name="TXN_XML")
private String txnXml;
#Column(name="UNIT_ID")
private String unitId;
#Column(name="VENDOR_APP_ID")
private String vendorAppId;
#Column(name="G_NUM")
private String gNum;
#Column(name="XTRAC_WORKITEM_ID")
private String xtracWorkitemId;
public Transaction() {
}
//getters and setters
Here is the Application Entity
#Entity
#NamedQuery(name="Application.findAll", query="SELECT a FROM Application a")
public class Application implements Serializable {
private static final long serialVersionUID = 1L;
#EmbeddedId
private ApplicationPK id;
#Temporal(TemporalType.DATE)
#Column(name="AP_DT")
private Date apDt;
#Column(name="DTCC_APPSUB_XML")
private String dtccAppsubXml;
#Column(name="PROCESSING_CODE")
private String processingCode;
#Column(name="TXN_ID")
private String txnId;
public Application() {
}
//getters and seters
And here is the POJO I am trying to populate with the query.
public class AnnuityNetPdfToFims
{
private String confirmationId;
private String taxId;
private Date apDt;
private long fimsReturnNum;
private String txnId;
//getters and setters
So I can see that the data is coming back but it is coming back as an Object []. I suppose I could just use that to populate the POJO but I don't think that's very reliable. I have heard that there is a way to use the #SqlResultSetMapping annotation, but have not been able to use it successfully yet. I have also heard that i may be able to specify the actual name of the object in the query like so: How to map a native query to POJO class using jpa and hibernate.
However I am not able to get this to work either. Any suggestions? Thanks in advance.

I want to create an entity X

I want to create an entity X with atributes.
Everything is right except the attribute "permissions" :
public class X implements Serializable{
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long idNotateur;
#NotEmpty
private String nomNotateur;
#NotEmpty
private String prenomNotateur;
#NotEmpty
private String fonctionNotateur;
#NotEmpty
private String userNotateur;
#NotEmpty
private String passNotateur;
#ManyToOne
#JoinColumn(name="id_poste")
private Poste poste;
#ManyToOne
#JoinColumn(name="id_dir")
private Direction direction;
#OneToMany(mappedBy="notateur")
private Collection<Employe> Employes;
private Collection<Long> permissions;
getters & setters ...
public Collection<Long> getPermissions() {
return permissions;
}
public void setPermissions(Collection<Long> permissions) {
this.permissions = permissions;
}
}
Then I came across the following error: Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Collection, at table: X, for columns: [org.hibernate.mapping.Column(permissions)]
So how to solve it?
I'm using Spring MVC Hibenate

Resources