Spring data r2dbc blob - Blob data handling - spring-data

I am using Reactive Spring Boot 2.5.4 with MySQL data source. I am not sure how to read/write blob with Spring data r2dbc.
Table - user_note
Columns - id, name, description
Here, description column is a blob data type.
My Domain class:
class UserNote {
private final Integer id; private final String name; private final String description;
// Constructor & Getter goes here }
Repository class,
public class UserNoteRepository extends ReactiveCrudRepository<UserNote, Integer> {
}
In my handler class, I am getting user input and creating domain and reading data back from the repository. Not sure how to handle the Blob data type with Spring data r2dbc.

Related

JPA - perform an insert on a Postgres table whose primary key is generated from a database trigger

I am writing an API where I am inserting a record into a table (Postgres). I was hoping to use JPA for the work. Here is the potential challenge: the primary key for the insert is generated from a database trigger, rather than from sequence count or similar. In fact, the trigger creates the primary key using the values of other fields being passed in as part of the insert. So for example,
if I have a entity class like the following:
#Entity
#Validated
#Table(name = "my_table", schema="common")
public class MyModel {
#Id
#Column(name = "col_id")
private String id;
#Column(name = "second_col")
private String secCol;
#Column(name = "third_col")
private String thirdCol;
public MyModel() {
}
public MyModel(String id, String secCol, String thirdCol) {
this.id = id;
this.secCol = secCol;
this.thirdCol = thirdCol;
}
}
I would need the col_id field to somehow honor that the key is generated from the trigger, and the trigger would need to be able to read the values for second_col and third_col in order to generate the primary key. Finally, I would need the call to return the value of the primary key.
Can this be done with jpa and repository interface such as:
public interface MyRepo extends JpaRepository <MyModel, String> {
}
and then use either default save method such as myRepo.saveAndFlush(myModel) or custom save methods? I can't find anything on using JPA with DB triggers that generating keys. If it cannot be done with JPA, I would be grateful for any alternative ideas. Thanks.
ok, I was able to get this to work. It required writing a custom query that ignored the primary key field:
public interface MyRepo extends JpaRepository <MyModel, String> {
#Transactional
#Modifying
#Query(value = "INSERT INTO my_table(second_col, third_col)", nativeQuery = true)
int insertMyTable(#Param("second_col") String second_col, #Param("third_col") String third_col);
}
The model class is unchanged from above. Because it was executed as a native query, it allowed postGres to do its thing uninterrupted.

Spring Data JPA : How save data into database using save() of jpaRepository

I created one spring Application. I am trying to save data into database using save method of JPA Repository. i am getting Error null value in column "id" violates not-null constraint
HomeController
#RestController
public class HomeController
{
#Autowired
public userRepository repository;
#RequestMapping(value="/save2",method=RequestMethod.POST )
public String save1(#ModelAttribute user us)
{
repository.save(us);
return "sucessfull";
}
}
user
#Entity
#Table(name="user", schema="new")
public class user implements Serializable
{
private static final long serialVersionUID = -2956665320311624925L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
#Column(name="uname")
public String uname;
#Column(name="pass")
public String pass;
Table Script
Through Postman I am trying to Insert following data
I am getting this error
org.postgresql.util.PSQLException: ERROR: null value in column "id" violates not-null constraint
Can Any one tell me what i am doing wrong in above code
I see couple of issues here.
First, replace your #ModelAttribute with #RequestBody since you're sending a JSON request, it is wise to use the latter. (Read up here and here). In your case, the values from request is not passed to repository save method including Id value. That's the reason you're getting not null constraint error.
Second, since you're using GenerationType.IDENTITY strategy, you should use serial or bigserial type to let Postgres to generate your primary key.
Read up nicely written answers on IDENTITY strategy here
You defined id as an Integer field in your model class. Try to pass the value in the json as an Integer, not as a String.
{
"id": 1,
"uname": "abc",
"upass": "abc"
}

Does Samza's OutgoingMessageEnvelope require a SerDe for partitionKey and how do I specify it?

Similar to how-can-you-create-a-partition-on-a-kafka-topic-using-samza I need to construct a message controlling how it's routed via use of partitionKey. key and message do require a SerDe but I'm not sure if partitionKey does as well. If so what is the configuration syntax? I suppose I can rely on Samza internally using key as the partitionKey but I'd rather keep them explicit and separate.
From the source code it doesn't seem like partition key requires a SerDe - note key and message have a corresponding xxxxSerializerName
public class OutgoingMessageEnvelope {
private final SystemStream systemStream;
private final String keySerializerName;
private final String messageSerializerName;
private final Object partitionKey;
private final Object key;
private final Object message;

SpringData MongoDB cannot determine IsNewStrategy during Auditing

I am trying to enable Auditing using Annotations. My domain class has #Id field that is populated while constructing the object. I have added a java.util.Date field for lastModified and annotated it with #LastModifiedDate.
#Document
public class Book {
#Id
private String name;
private String isbn;
#LastModifiedDate
private Date lastModified;
public Book(String name) {
this.name = name;
}
}
I have enabled auditing in the Spring Configuration XML using <mongo:auditing/>.
When I try to save an instance of my object, I get the following error:
Book book1 = new Book("ABCD");
mongoOps.save(book1);
java.lang.IllegalArgumentException: Unsupported entity com.pankaj.Book! Could not determine IsNewStrategy.
I do not want to use the Auditable interface nor extend my domain classes from AbstractAuditable. I only want to use the Annotations.
Since I am not interested in the #CreatedBy and the #LastModifiedBy, I am not implementing the AuditAware interface as well.
I just want the #LastModifiedDate to work for my domain classes. What am I missing?
I am using version 1.7.0 of SpringData MongoDB.
You don't mention how you are configuring your MongoDB connection but if you are using AbstractMongoConfiguration, it will use the package of the actual configuration class to look for #Document annotated classes at startup.
If your entities are in a different package, you will have to manually hand that package by overriding AbstractMongoConfiguration.getMappingBasePackage(). Placing this in you Mongo Configuration class should do the trick (again, this is considering you are extending AbstractMongoConfiguration for your Mongo configuration):
#Override
protected String getMappingBasePackage() {
return "package.with.my.domain.classes";
}
I had same issue, later I determined that I was missing ID field with annotation;
#Id
private String Id
in my class I was trying to persist with
#Document(collection="collectionName")
I had the same issue when using annotations only configuration.
When you put #EnableMongoAuditing on a configuration class, Spring will create a MappingContext bean.
Then you have to make sure the same mappingContext is being used in the MongoTemplate.
#Configuration
#EnableMongoAuditing
#EnableMongoRepositories(value = "my.repositories.package", mongoTemplateRef = "myMongoTemplate")
class MongoConfig {
#Autowired
//Autowiring the MongoMappingContext will supply the same MongoMappingContext as the one used in auditing
MongoMappingContext mongoMappingContext;
#Bean
MongoTemplate myMongoTemplate() {
String databaseName = "mydbname";
MongoDbFactory factory = new SimpleMongoDbFactory(mongoClient, databaseName);
MongoConverter converter = new MappingMongoConverter(factory, mongoMappingContext);
MongoTemplate mongoTemplate = new MongoTemplate(factory, converter);
return mongoTemplate;
}
}
My project running in version 1.6.2 runs normally, except that # LastModifiedDate does not update. After I updated to version 1.7.1. I had the same problem as you.
I tried to implement the class: org. Springframework. Data. Domain. The Auditable this interface, seemingly can preserve the normal, but the createdBy and createdDate two fields could not be saved to the database.
I had the same issue and fixed it by extending the Document class with AbstractPersistable. In you case it can be
public class Book extends AbstractAuditable

javaee 6 rest api named query result

I have a simple JEE6 rest class that gets the data from db2. I am using Jackson in ApplicationConfig class to convert the entity objects to json. It converts with the field names as the key and the value as the right hand value. So for example:
Class Entity {
String name;
String address;
}
converts to
{name:"hello", address:"world"}
The service is as follows:
public List<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
Now I want to only return the name in json format. So I created a named query as follows in the entity class:
#NamedQuery(name = "justGetName", query = "SELECT a.name FROM Applications a")
And the service changed to
public List<T> findAll() {
return getEntityManager().createNamedQuery("justGetName").getResultList();
}
This returns the following array:
[{"first","second","third"}]
But I want to get back:
[{name:"first",name:"second",name:"third"}]
How do I write the named query so that the class field names are added to the json structure? Thank you.
You querying a list of strings from your database and this is what the service returns.
Their are multiple ways to achieve your goal.
Pure JPA
Using #JsonIgnore to tell Jackson not to serialize an attribute
class Application {
String name;
#JsonIgnore
String address;
}
Create a new Entity class that only contains the attributes you would like to share
class ApplicationName {
String name;
}
Alternatively you could introduce a separate class that only contains the attributes you would like to share and convert the results from the query into this class and return than the list of this converted values.