Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 775 Vote(s) - 3.4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Room Persistence: Error:Entities and Pojos must have a usable public constructor

#11
For me all I had to do was to add a constructor to the data class with empty params sent to it like so:

@Entity(tableName = "posts")
data class JobPost(
@Ignore
@SerializedName("companyLogo")
var companyLogo: String,
@Ignore
@SerializedName("companyName")
var companyName: String,
@Ignore
@SerializedName("isAggregated")
var isAggregated: String,
@PrimaryKey(autoGenerate = false)
@SerializedName("jobID")
var jobID: String,
@Ignore
@SerializedName("jobTitle")
var jobTitle: String,
@Ignore
@SerializedName("postedOn")
var postedOn: String,
@Ignore
@SerializedName("region")
var region: String
) {
constructor() : this("","","","","","","")
}
Reply

#12
Same bug, much stranger solution: Do not return cursor using reactivex `Maybe<Cursor>` on your Dao. `Flowable`, `Single`, and `Observable` did not work either.

Simply bite the bullet and make the reactive call outside the Dao request.
Before:

@Dao
interface MyDao{
@Query("SELECT * FROM mydao")
fun getCursorAll(): Flowable<Cursor>
}

After:

@Dao
interface MyDao{
@Query("SELECT * FROM mydao")
fun getCursorAll(): Cursor
}
Meta:

Android Studio 3.2
Build #AI-181.5540.7.32.5014246, built on September 17, 2018
JRE: 1.8.0_152-release-1136-b06 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.12.6
Reply

#13
Just add the below annotation to any constructor that causes the errors and add a new blank constructor.

> @Ignore
Reply

#14
As stated in [Room Database `Entity`][1]:

> Each entity must either have a no-arg constructor or a constructor
> whose parameters match fields (based on type and name).

So adding an empty constructor and annotating your parameterized constructor with `@Ignore` will solve your problem. An example:

public class POJO {

long id;

String firstName;

@Ignore
String lastName;

public POJO() {
}

@Ignore
public POJO(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

// getters and setters
// ...

}

[1]:

[To see links please register here]

Reply

#15
Don't use to dataclass,use normal class instead. This method will solve problem
Reply

#16
With 2.1.0-alpha6, it turned out to be an invalid return type in Dao. Fixing the return type as expected fixed it.
Reply

#17
For me, I was using 'lat' & 'long' as a variable name in the data(Entity) class for kotlin so renaming to latitude & longitude it worked.

**Not working:**

@Entity(tableName = "table_User")
data class User(@PrimaryKey var userId : Int, @ColumnInfo(name = "first_name")
var firstName: String
, @ColumnInfo(name = "last_name") var lastName: String
, @ColumnInfo(name = "password") var password: String
, @ColumnInfo(name = "dob") var dob: Long
, @ColumnInfo(name = "address") var address: String
, @ColumnInfo(name = "lat") var latitude: Double
, @ColumnInfo(name = "long") var longitude: Double) {

}

**Working:**

@Entity(tableName = "table_User")
data class User(@PrimaryKey var userId : Int, @ColumnInfo(name = "first_name")
var firstName: String
, @ColumnInfo(name = "last_name") var lastName: String
, @ColumnInfo(name = "password") var password: String
, @ColumnInfo(name = "dob") var dob: Long
, @ColumnInfo(name = "address") var address: String
, @ColumnInfo(name = "latitude") var latitude: Double
, @ColumnInfo(name = "longitude") var longitude: Double) {

}
Reply

#18
For a variation on FutureShocked answer that implements autoGenerate:

@Entity(tableName = "movies")
data class MovieKt(
var title: String,
var overview: String,
var poster_path: String,
var backdrop_path: String,
@Ignore var release_date: String,
@Ignore var vote_average: Double,
@Ignore var isFavorite: Int
) {
@PrimaryKey(autoGenerate = true) var id : Int = 0

constructor(title: String, overview: String, poster_path: String, backdrop_path: String) {
this(id, title, overview, poster_path, backdrop_path, "", 0.0, 0)
}
}
Reply

#19
Like it's said in the Room docs, you are **required to make an empty public constructor**. At the same time, if you want to declare other custom constructors, you must add `@Ignore` annotation.

@Entity
public class CartItem {
@PrimaryKey
public int product_id;
public int qty;

public CartItem() {
}

@Ignore
public CartItem(int product_id, int count) {
this.product_id = product_id;
this.qty = count;
}
}
Reply

#20
It's not a problem in your case, but for others, this error can occur if you have @Ignore params in your primary constructor, i.e. Room expects to have either:

- parameterless constructor or
- constructor with all fields not marked with @Ignore

for example:

@Entity(tableName = "movies")
data class MovieKt(
@PrimaryKey
var id : Int,
var title: String,
@Ignore var overview: String)

will not work. This will:

@Entity(tableName = "movies")
data class MovieKt(
@PrimaryKey
var id : Int,
var title: String)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through