I'm having this error when trying to compile the project:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
java.lang.IllegalArgumentException: intcannot be converted to an Element
And this warning:
Warning:Supported source version 'RELEASE_7' from annotation processor 'android.arch.persistence.room.RoomProcessor' less than -source '1.8'
These are my database related classes:
#Entity(tableName = "users")
public class SerializedUser {
#PrimaryKey(autoGenerate = true)
private int id;
#ColumnInfo(name = "first_name")
private String firstName;
#ColumnInfo(name = "last_name")
private String lastName;
#ColumnInfo(name = "username")
private String username;
public SerializedUser(int id, String firstName, String lastName, String username) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.username = username;
}
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getUsername() {
return username;
}
}
#android.arch.persistence.room.Database(entities = {SerializedUser.class}, version = 4)
public abstract class Database extends RoomDatabase {
public abstract UserDao userDao();
private static final String DATABASE_NAME = "weather";
// For Singleton instantiation
private static final Object LOCK = new Object();
private static volatile Database i;
public static Database init(Context context) {
if (i == null) {
synchronized (LOCK) {
if (i == null) {
i = Room.databaseBuilder(context.getApplicationContext(),
Database.class, Database.DATABASE_NAME)
.fallbackToDestructiveMigration().build();
}
}
}
return i;
}
public static boolean isInited(){
return i != null;
}
public static Database getInstance(){
if(i == null)
throw new NullPointerException("Database.getInstance called when Database not initialized");
return i;
}
}
#Dao
public abstract class UserDao {
Converters converters;
public void inject(Converters converters) {
this.converters = converters;
}
#Insert(onConflict = REPLACE)
public abstract void saveNow(SerializedUser user);
#Delete
public abstract void deleteNow(int id);
#Query("DELETE FROM users")
public abstract void deleteAllNow();
#Query("SELECT * FROM users")
public abstract List<SerializedUser> getAllNow();
#Query("SELECT * FROM users ORDER BY last_name ASC")
public abstract LivePagedListProvider<Integer, SerializedUser> usersByLastName();
}
So the error happens when it tries to implement the database classes? That id on SerializedUser is not the problem, I have commented it out, the problem was still the same. Tried to clean and rebuild the project and restarted Android Studio (invalidate and restart).
remove
#Delete
public abstract void deleteNow(int id);
from your Dao it will work
#Delete annotation marks a method in a Dao annotated class as a
delete method. The implementation of the method will delete its
parameters from the database.
All of the parameters of the Delete method must either be classes annotated with Entity or collections/array of it.
Read here for extra information.
So in your case you pass a parameter with int type which violates the aforementioned rule. That is why you are getting that error.
In order to resolve this issue, you should either exclude deleteNow method or just pass any parameter that does not violates the rule that was mentioned above.
Error:Execution failed for task ':app:compileDebugJavaWithJavac'. java.lang.IllegalArgumentException: intcannot be converted to an Element
Basically, this problem arises not only by the #Delete query, but by all the Room's CRUD annotations (#Insert, #Delete, #Update) except #Query.
All of the parameters of these CRUD annotated methods must either be classes annotated with Entity or collections/array of it.
So we can't pass primitive or other than these.
Related
This question already has an answer here:
Internal working of field injection in spring and why is it not recommended to use
(1 answer)
Closed 6 months ago.
I am very new to Spring Boot. I have created a repository, which looks like this:
public interface UserRepository extends CrudRepository<User, Long> {
List<User> findByEmail(String email);
User findById(long id);
}
user.java
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String email;
private String password;
private boolean verified;
protected User() {}
public User(String email, String password, boolean verified) {
this.email = email;
this.password = password;
this.verified = verified;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isVerified() {
return verified;
}
public void setVerified(boolean verified) {
this.verified = verified;
}
}
And now I want to inject the repository into my controller. This is how I tried it:
#RestController
public class Register {
#Autowired
private UserRepository userRepository;
#PostMapping("/register")
public User registerUser() {
return userRepository.save(new User("test#example.com", "password", true));
}
}
Is my approach correct? If so, then why do I get a warning on #Autowired that says:
Field injection is not recommended
?
You are applying field injection into your controller layer.
There is a more efficient way to do that but first, take a look at why field injection is not recommended.
Reasons
you can not define your dependent components as final(immutable).
you can not instantiate your components for test purposes.
your application may lead to circular dependencies.
your application is tightly coupled to your container.
There may be more reasons that you can search for them if you want. So now take a look at better ways to perform dependency injection.
Constructor Injection
This purpose is just to define your required dependencies as parameters to the class's constructor.
Let's say we have a class called A
#Component
Class A {}
Notice that we have to define our class as a Component so the container can use it later.
Now we want to inject class A to class B with constructor injection purpose.
Class B {
private final A a;
Public B(A a) {
this.a = a;
}
}
We successfully performed constructor injection instead of field injection.
There is another way to do dependency injection called setter injection which is useful for injecting optional dependent components.
That's it.
I use MongoDBRepository in spring boot, and when I save some object in database everything is ok. but when I find object by id spring does not allow do that.
I try to change VehicleRoutingProblemSolution type to Object type, but VehicleRoutingProblemSolution have other object field PickupService and it without default constructor to. And yes, this class has immutable... I can't create default constructors, what can I do?
import com.fasterxml.jackson.annotation.JsonProperty;
import com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
#Document(collection = "vrp_solutions")
public class VrpSolutionHolder {
// Specifies the solution id
#Id
#JsonProperty("id")
private String id;
// Specifies the solution id
#JsonProperty("solution")
private VehicleRoutingProblemSolution vehicleRoutingProblemSolution;
// Created at timestamp in millis
#JsonProperty("created_at")
private Long created_at = System.currentTimeMillis();
public VrpSolutionHolder(String id, VehicleRoutingProblemSolution vehicleRoutingProblemSolution) {
this.id = id;
this.vehicleRoutingProblemSolution = vehicleRoutingProblemSolution;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public VehicleRoutingProblemSolution getVehicleRoutingProblemSolution() {
return vehicleRoutingProblemSolution;
}
public void setVehicleRoutingProblemSolution(VehicleRoutingProblemSolution vehicleRoutingProblemSolution) {
this.vehicleRoutingProblemSolution = vehicleRoutingProblemSolution;
}
public Long getCreated_at() {
return created_at;
}
public void setCreated_at(Long created_at) {
this.created_at = created_at;
}
}
org.springframework.web.util.NestedServletException: Request
processing failed; nested exception is
org.springframework.data.mapping.model.MappingInstantiationException:
Failed to instantiate
com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution
using constructor NO_CONSTRUCTOR with arguments
I ran into the exact same problem. A persistent immutable class containing other class instances, throwing that aforementioned exception when retrieved by this repository method:
public interface ProjectCodeCacheRepository extends MongoRepository<CachedCode, String> {
public CachedCode findByCode(String code);
public List<CachedCode> findByClientId(UUID clientId);
}
...
List<CachedCode> cachedForClient = this.codeCacheRepo.`**findByClientId**`(clientId);
...
Following Erwin Smouts hints, this is nicely fixed by giving it a special constructor annotated org.springframework.data.annotation.PersistenceConstructor like so:
#Document(collection="cachedcodes")
public class CachedCode {
#PersistenceConstructor
public CachedCode(String code, UUID clientId, LocalDateTime expiration) {
this.code = code;
this.clientId = clientId;
this.expiration = expiration;
}
public CachedCode(String code, UUID clientId, long secondsExpiring) {
this.code = code;
this.clientId = clientId;
this.expiration = LocalDateTime.now().plusSeconds(secondsExpiring);
}
public UUID getClientId( ) {
return this.clientId;
}
public String getCode() {
return this.code;
}
public boolean hasExpired(LocalDateTime now) {
return (expiration.isBefore(now));
}
...
#Id
private final String code;
private final UUID clientId;
private final LocalDateTime expiration;
}
So, you should check if your VehicleRoutingProblemSolution has a) a constructor that matches the database fields (check in mongo client) and b) is annotated to be the one used by the driver (or whichever piece of Spring magic under the hood).
If your framework tool requires (visible) no-arg constructors (plus accompanying setters), and the class you have is required to stay as is, then you could roll your own, say, MutableVehicleRoutingProblemSolution where in the setters you could have :
this.vehicleRoutingProblemSolution = new VehicleRoutingProblemSolution(vehicleRoutingProblemSolution.getId(), newSolution);
Thus your MutableVehicleRoutingProblemSolution wraps around the existing VehicleRoutingProblemSolution.
Hacky smell to it, but it fits the requirements.
(Or you could try to find a tool that is able to use, not annotations on the contained fields, but annotations on constructor arguments.)
This is a problem where the corresponding class does not have a no-arg constructor like - I was facing an issue with java.io.File.
Solution:
In general - change the declaration to Object class and convert where we are using the class.
from
class MyClass{
File myfile;
}
to
class MyClass{
Object myFile;
}
For anyone using lombok, you need to remove the #Builder annotation on your class and use #Data instead, or follow the above solution to provide a specialized constructor
Oddly, I received this when I attempted to decorate a custom interface with ...
#Document(collection = "Person")
Example:
package test.barry.interfaces;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.UpdateDefinition;
#Document(collection = "Person")
public interface CustomRepository
{
void updatex(Query filterPredicate, UpdateDefinition updatePredicate);
}
I want to load any property files in spring POJO classes.
Actually, I want to use any properties from property file in #Size, #NotNull or any validation annotations in spring model classes.
But the issue is that #Size, #NotNull etc annotation are invoked at compile-time whereas any property file's data are invoked at runtime.
Even, I want to use in the max parameter of #Size which accepts the only constant integer. How can I load value and cast it to Integer as a constant value?
And I can use custom validator or any custom annotation to solve the issue but as per the organization policy, I can't use much customization.
I found an additional solution to this problem that we can use a class of constants instead of the property file.
I can use this constants file anywhere in my workspace whereas property file can't use at Size.max property of the model validation
CommonConstants.java
public class CommonConstants
{
public static final int NAME=4;
public static final int ROLE=2;
}
Employee.java
private int id;
#Size(max = CommonConstants.NAME, message = "length exceeds : name")
private String name;
#Size(max = CommonConstants.ROLE, message = "length exceeds : role")
private String role;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
EmployeeController.java
#Controller
public class EmployeeController {
private static final Logger logger = LoggerFactory.getLogger(EmployeeController.class);
private Map<Integer, Employee> emps = null;
public EmployeeController() {
emps = new HashMap<Integer, Employee>();
}
#RequestMapping(value = "/emp/save.do", method = RequestMethod.POST)
public String saveEmployeeAction(#Valid Employee employee, BindingResult bindingResult, Model model)
{
if (bindingResult.hasErrors()) {
logger.info("Returning empSave.jsp page");
return "empSave";
}
logger.info("Returning empSaveSuccess.jsp page");
model.addAttribute("emp", employee);
emps.put(employee.getId(), employee);
return "empSaveSuccess";
}
}
Output
I have a Mcq class associated to a MongoRepository, and I want to get an instance of my Mcq which apply several changes (Answers shuffle, Questions draw, etc). I declared my function "myMcq.getInstance()", but I can't do that because every time I want to send a Mcq in a ResponseEntity there is an error in the JSON output because Springboot thinks that there is a "instance" property in my class.
Here is my java class :
#Document(collection = "Mcqs")
public class Mcq {
#Id public String id;
#DBRef public User creator;
public String title;
public String categoryID;
public List<McqChapter> chapterList = new ArrayList<>();
public Difficulty difficulty;
public Mcq() {}
public Mcq(String title) {
this();
this.title = title;
}
public ArrayList<String> getQuestionsIDs() {
ArrayList<String> result = new ArrayList<>();
for (McqChapter chapter : chapterList) result.addAll(chapter.getQuestionIDs());
return result;
}
public McqInstance getInstance() {
return new McqInstance(this);
}
}
To prevent the error add #JsonIgnore to getInstance() method:
#JsonIgnore
public McqInstance getInstance() {
return new McqInstance(this);
}
Marker annotation that indicates that the annotated method or field is to be ignored by introspection-based serialization and deserialization functionality. That is, it should not be consider a "getter", "setter" or "creator".
I've been using Spring Data for saving entities to the mongo DB and my code at the moment looks like this:
I have a repo class:
public interface LogRepo extends MongoRepository<Log, String> {
}
and I have an Entity Log which looks like this:
#Document(
collection = "logs"
)
public class Log {
#Id
private String id;
private String jsonMessage;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getJsonMessage() {
return jsonMessage;
}
public void setJsonMessage(String jsonMessage) {
this.jsonMessage = jsonMessage;
}
}
and this work well for me, however this approach works only for the case if I want to save Log entities to "logs" collection. However it would be very nice for me to be able to save Log entity to different collections depending on the context. I mean it would be nice to define collection name in the runtime. Is it possible somehow?
Thanks, cheers
Try to use inheritance and define appropriate collection names in such way. May give you possibility to save in different collections but you will be still not able to specify dynamically collection names and resp. their amount at runtime.
#Document(
collection = "logs"
)
public class Log {
#Id
private String id;
private String jsonMessage;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getJsonMessage() {
return jsonMessage;
}
public void setJsonMessage(String jsonMessage) {
this.jsonMessage = jsonMessage;
}
}
#Document(
collection = "log_child"
)
public class LogChild extends Log{}
With the MongoOperations save method you can choose which class to use and
based on the class it will choose the appropriate collection.
#Document(collection = "collection_#{T(com.github.your_project.DBUtils).getCollectionName()}")
public Class Collection
You can change the name in real time using a static getter
#UtilityClass
public class DBUtils {
private String collectionName;
public String getCollectionName() {
return collectionName;
}
public void setCollectionName(String collectionName) {
DBUtils.collectionName = collectionName;
}
}