This is my Entity "WebhookType":
public class WebhookType extends PanacheMongoEntity {
public ObjectId id;
public String type;
public String name;
public String description;
public String image;
public List<WebhookTypeParameter> webhookTypeParameters = new ArrayList<>();
}
Which is having a list of "WebhookTypeParameters":
public class WebhookTypeParameter {
public Enum<ParameterType> parameterType;
public String parameterName;
public String parameterExample;
public WebhookTypeParameter(){
}
public Enum<ParameterType> getParameterType() {
return parameterType;
}
public void setParameterType(Enum<ParameterType> parameterType) {
this.parameterType = parameterType;
}
public String getParameterName() {
return parameterName;
}
public void setParameterName(String parameterName) {
this.parameterName = parameterName;
}
public String getParameterExample() {
return parameterExample;
}
public void setParameterExample(String parameterExample) {
this.parameterExample = parameterExample;
}
public WebhookTypeParameter(String parameterName, Enum<ParameterType>parameterType, String parameterExample){
this.setParameterName(parameterName);
this.setParameterType(parameterType);
this.setParameterExample(parameterExample);
}
}
Which are having a field "parameterType" of Type Enum:
public enum ParameterType {
STRING, DOUBLE;
}
Now when trying to persist my entity like this:
WebhookType webhookType = new WebhookType();
webhookType.type = "XYZ";
webhookType.name = "XYZ";
webhookType.image = "image url";
webhookType.description = ("Lorem Ipsum");
webhookType.webhookTypeParameters.add(new WebhookTypeParameter("title", ParameterType.STRING, "titel xy"));
webhookType.webhookTypeParameters.add(new WebhookTypeParameter("name", ParameterType.STRING, "foobar"));
webhookType.persist();
I get this in my log:
2020-09-19 14:01:07,587 ERROR [io.qua.application] (Quarkus Main Thread) Failed to start application: org.bson.codecs.configuration.CodecConfigurationException: An exception occurred when encoding using the AutomaticPojoCodec.
Encoding a WebhookType: 'WebhookType<null>' failed with the following exception:
Failed to encode 'WebhookType'. Encoding 'webhookTypeParameters' errored with: An exception occurred when encoding using the AutomaticPojoCodec.
Encoding a WebhookTypeParameter: 'x.x.x.WebhookTypeParameter#474d3e58' failed with the following exception:
Failed to encode 'WebhookTypeParameter'. Encoding 'parameterType' errored with: Can't find a codec for class x.x.x.ParameterType.
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
So my question is what's wrong with my enum and why is it not automatically encoded?
Related
I'm using Micronaut Data JDBC and I'm facing an error. I have this entity:
#MappedEntity(value = "document_metadata")
#AllArgsConstructor
#EqualsAndHashCode
public class DocumentMetadataJDBCEntity implements DocumentMetadata {
#Embeddable
#AllArgsConstructor
public static class MetadataPk {
#MappedProperty(value = "document_uid")
#NotNull
private UUID documentUid;
#MappedProperty(value = "metadata_key")
#NotNull
private String metadataKey;
public UUID getDocumentUid() {
return documentUid;
}
public String getMetadataKey() {
return metadataKey;
}
}
#EmbeddedId
private MetadataPk metadataPk;
#NotNull
private String metadataValue;
public MetadataPk getMetadataPk() {
return metadataPk;
}
#Override
public String getMetadataKey() {
return getMetadataPk().getMetadataKey();
}
#Override
public String getMetadataValue() {
return metadataValue;
}
public UUID getDocumentUid() {
return getMetadataPk().getDocumentUid();
}
}
And when inserting I get this error:
io.micronaut.data.exceptions.DataAccessException: SQL error executing INSERT: Batch entry 0 INSERT INTO "document_metadata" ("metadata_key","metadata_value","document_uid","document_uid","metadata_key") VALUES ('id','1234','c960d8de-99a4-40a6-91bf-b0d4a73910d6'::uuid,'c960d8de-99a4-40a6-91bf-b0d4a73910d6'::uuid,'id') was aborted: ERROR: column "document_uid" specified more than once
The code for saving is the next one:
Set<DocumentMetadataJDBCEntity> metadataSet = metadata.entrySet().stream()
.map(e -> new DocumentMetadataJDBCEntity(new DocumentMetadataJDBCEntity.MetadataPk(
savedDocument.getUid(), e.getKey()), e.getValue())).collect(toSet());
Iterable<DocumentMetadataJDBCEntity> persistedMetadata = documentMetadataJDBCRepository.saveAll(metadataSet);
Any idea?
Add #Transient to your convenience accessor (getter) methods:
#Override
#Transient
public String getMetadataKey() {
return getMetadataPk().getMetadataKey();
}
#Transient
public UUID getDocumentUid() {
return getMetadataPk().getDocumentUid();
}
It "tells" Micronaut not to save the return value into the DB.
I have problem while casting an unknown enum to a default enum in spring boot while using mongo repository.
This is the enum.
public enum EventType implements Serializable
{
WORKDONE("WORKDONE"),
ODRCOM("ODRCOM"),
EXECUTED("EXECUTED"),
REBOOK("REBOOK"),
MANUAL("MANUAL"),
UNKNOWN("UNKNOWN");
private String value;
EventType(final String type) {
this.value = type;
}
#Override
public String toString() {
return value;
}
}
And here is my model class
#JsonInclude(JsonInclude.Include.NON_NULL)
public class Event {
//other properties
#JsonProperty("eventType")
private EventType eventType;
#JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
#JsonProperty("eventType")
public EventType getEventType() {
return eventType;
}
#JsonProperty("eventType")
public void setEventType(String eventType) {
this.eventType = Optional.ofNullable(EventType.valueOf(eventType)).orElse(EventType.UNKNOWN);
}
//other getters and setters
}
Here is the mongo repository
public interface EventRepository extends MongoRepository<Event, String> {
}
The document stored in the db is of the following stucture
{
...
"eventType" : "REBOOK1",
...
}
Please note that the REBOOK1 is not a valid enum. But the setter should be able to able to cast anything else to type UNKNOWN.
However it gives this exception everytime
No enum constant dk.nuuday.ossieventprocessor.app.model.EventType.REBOOK1
I have tried with adding a custom converter as a configuration but no luck
Any help is greatly appreciated
I have created a simple REST service (POST). But when I call this service from postman #RequestBody is not receiving any values & getting nullPointer.
#RequestMapping(value=/searchEmployee,method = RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
#Timed
public ResponseEntity<List<EmpDTO>> search(#RequestBody EmpSearchDTO empSearchDTO){
String brId=empSearchDTO.getBrid();// null
List<String> location=empSearchDTO.getLocation()//null
Employee e=employeeRepository.findByEmployeeBrId(String brId);
}
My java Bean object is like below:
public class EmpSearchDTO{
private String brId;
private List<String> location;
public void setBrid(String brId){
brId=brId;
}
public String getBrid(){
return brId;
}
public void setLocation(List<String location){
location=location;
}
public List<String> getLocation(){
return location;
}
}
JSON which I am passing in request body is
empSearchDTO
{
"brId":"G1234",
"location":["India"]
}
This is the call in the ProductServices.xml
<update id="resetPassword" parameterType="batchReport">
{ call user_account_mng.enc_reset_password(
#{user_Id,jdbcType=VARCHAR,mode=IN},
#{encrypted_password,jdbcType=VARCHAR,mode=IN},
#{usr_id, dbcType=VARCHAR,mode=IN},
#{salt,jdbcType=VARCHAR,mode=IN},
#{ret_code,jdbcType=CHAR,mode=OUT},
#{pgp_encrypted_password,jdbcType=BLOB,mode=IN}
)}
Now BatchReport is a POJO:
(i have declared an alias for it as batchReport)
public class BatchReport
{
private String user_Id;
private String encrypted_password;
private String usr_id;
private String salt;
private String ret_code;
private byte[] pgp_encrypted_password;
public String getUser_Id() {
return user_Id;
}
public void setUser_Id(String user_Id) {
this.user_Id = user_Id;
}
public String getEncrypted_password() {
return encrypted_password;
}
public void setEncrypted_password(String encrypted_password) {
this.encrypted_password = encrypted_password;
}
public String getUsr_id() {
return usr_id;
}
public void setUsr_id(String usr_id) {
this.usr_id = usr_id;
}
public String getSalt() {
return salt;
}
public void setSalt(String salt) {
this.salt = salt;
}
public String getRet_code() {
return ret_code;
}
public void setRet_code(String ret_code) {
this.ret_code = ret_code;
}
public byte[] getPgp_encrypted_password() {
return pgp_encrypted_password;
}
public void setPgp_encrypted_password(byte[] pgp_encrypted_password) {
this.pgp_encrypted_password = pgp_encrypted_password;
}
}
My main class is like this :
<BatchReport batchReport = new BatchReport();
byte[] byteArray =new byte[]{1,2,3};
batchReport.setUser_Id("CHI");
batchReport.setEncrypted_password("97D6B45");
batchReport.setSalt("71L");
batchReport.setPgp_encrypted_password(byteArray);
String returnCode = productServiceObj.resetPassword(batchReport);
i am getting following error:
Error setting null parameter. Most JDBC drivers require that the JdbcType must be specified for all nullable parameters. Cause: java.sql.SQLException: Invalid column type
The error may involve com.example.services.ProductServices.resetPassword-Inline
ProductServices is a class in which the method resetPassword is declared.
Please help me with this BLOB issue.
What should be the jdbcType in the called procedure.
what value should be passed in this pgp_encrypted_password.
Okay I found the solution to the problem now the jdbcType in the query in .xml file remains the same i.e BLOB.
Next the type which gets set for passing in the values is byte[].
So everything remains same as i have covered up .
Error actually existed as the in .xml file returns an integer indicating the number of rows changed in query and I have given the function return type as String so here goes the solution for the problem it should be of type Object.
I'm trying to extend the base ORMLite DAO class so I can add some custom methods. I've tried following the answer here, but I'm getting a null error and not sure how to cast the dao object correctly (Ormlite - Constructor call failing when BaseDaoImpl is extended) Currently, I have the following table:
#DatabaseTable(tableName="beers", daoClass=BeerDao.class)
public class Beer {
public static final String BEER_NAME = "name";
#DatabaseField(generatedId = true)
private UUID id = UUID.randomUUID();
#DatabaseField()
private String name;
#DatabaseField()
private String breweryName;
public Beer() {}
... getters/setters
}
The BeerDao class:
public class BeerDao<Beer,UUID> extends BaseDaoImpl<Beer,UUID> {
public BeerDao(ConnectionSource connectionSource, Class<Beer> dataClass) throws SQLException {
super(connectionSource, dataClass);
}
}
DatabaseHelper code:
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private static final String TAG = "Database";
private static final String DATABASE_NAME = "brewgenius.db";
private static final int DATABASE_VERSION = 7;
private BeerDao<Beer, UUID> beerDao = null;
private Dao<Checkin, UUID> checkinDao = null;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
}
/* ... onCreate and onUpgrade code ..*/
/**
* Get Beer Model DAO
*
* #return Beer DAO
*/
public BeerDao<Beer, UUID> getBeerDao() {
if (beerDao == null) {
try {
beerDao = getDao(Beer.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
return beerDao;
}
}
In my activity, when I try to get the Dao, I get a null pointer exception.
BeerDao<Beer,UUID> dao = getHelper().getBeerDao();
FYI, Dao is cast to:
BeerDao<Beer,UUID>
UPDATE
It looks like my DatabaseConfigUtil isn't reading the DaoClass attribute.
public class DatabaseConfigUtil extends OrmLiteConfigUtil {
public static void main(String[] args) throws SQLException, IOException {
writeConfigFile("ormlite_config.txt");
}
}
Removing ormlite_config.txt from DatabaseHelper's constuctor caused on-the-fly reading which correctly reads the DaoClass attribute. Any idea why the writer isn't writing this?
Here's ormlite_config.txt
# --table-start--
dataClass=com.brewgenius.model.Beer
tableName=beers
# --table-fields-start--
# --field-start--
fieldName=id
columnName=_id
generatedId=true
# --field-end--
# --field-start--
fieldName=name
# --field-end--
# --field-start--
fieldName=breweryName
# --field-end--
# --table-fields-end--
# --table-end--
#################################