In my JPA code i use:
#MappedSuperclass
public abstract class BaseEntityVotes<T> extends BaseEntity {
/**
* The entity this vote belongs to.
*/
#ManyToOne
#NotNull
private T entity;
But Eclipse can't validate: "Target entity "java.lang.Object" is not an Entity".
(no error in IntelliJ)
I tried to change the type parameter to <T extends BaseEntity> (BaseEntity is also a MappedSuperclass) but this only changes the error text accordingly. Is this a real problem? Or is this a bug in eclipse?
In this regard i found a fixed bug. But this didn't helped. So is there a fix or work around to get rid of this error or can i fix the code in some way?
Related
Lombok: 1.16.18
Eclipse: Oxygen
I have an object with #Getter and #Setter as a base object:
#Getter
#Setter
#EqualsAndHashCode(of="uuid")
public abstract class BaseEntity implements Serializable,Auditable {
Auditable requires setCreatedBy, which is included in BaseEntity.
However, the following object, which extends BaseEntity, receives an error of "The type Login must implement the inherited method Auditable.setCreatedBy(String)"
#Getter
#Setter
public class Login extends BaseEntity{
Looking on the outline pane, setCreatedBy is properly generated on BaseEntity, but does not exist on Login. What am I configuring wrong?
Not sure what happened, but a full rebuild eventually fixed it. Not sure that was the issue though, as I had done several full builds since upgrading lombok.
I have created a webapp similiar to the tutorial here:
https://spring.io/guides/tutorials/react-and-spring-data-rest/ .
I have added postgresql db and everything works fine. I have a basic query findByUsername(String name) in my repository which works fine. My problem is, I am for some reason unable to create custom queries e.g
"SELECT CURRENT_TIMESTAMP".
Lets say i make a test where I just want to get the value of this statement. And by unable I mean I don't know how :)
My google results suggested my to add this to my class
#Autowired
EntityManager entityManager;
and then create queries on that. But entityManager is never initialized for some reason, return null always. Is this the right way and I'm just missing something? Also tried to add spring-boot-starter-jdbc and use JdbcTemplate but was also null.
EDIT
My problem was too little knowledge of Spring.
I had a class like this and tried to use Autowire in there.
public class Person {
#Autowire
MyRepo myRepo;
private String p;
public Person(String p) {
this.p = p;
}
As I understood later, Spring doesn't pick this up. I called out this class in a RestController with Person per = new Person("string");.
The solution I made was to Autowire MyRepo in my RestController class and my Person contstructor looked like this
public Person(String p, MyRepo myRepo) {
this.p = p;
this.myRepo = myRepo;
}
Removed the autowire in Person class. This way I managed to get myRepo initialized in Person class and use it there. This isn't the best soution for my problem.
My second problem. After i got this working, I tried to autowire EntityManager the same to my Person class. Just replaced MyRepo with EntityManager and it was also initialized. Now the problem is with my next code line Integer i = (Integer) em.createNativeQuery("select 1", Integer.class).getSingleResult();. Here I get an error javax.persistence.PersistenceException: org.hibernate.MappingException: Unknown entity: java.lang.Integer.
The purpose of this statement is to make a simple query to my DB, to if it responds.
You don't need to autowire the EntityManager, Spring takes care of all this for you.
Instead in your repository you need to define a new method and annotate it with #Query.
Example:
#Query(value = "select current_timestamp from #{#entityName} u")
String findTimestamp();
You can find furhter information about using #Query in the spring docs: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query.
The final solution was:
Integer i = (Integer) em.createNativeQuery("select 1").getSingleResult();
Just removed the Integer.class part from NatviveQuert. I guess, if I had wanted it to be there, I should have made a new bean for Integer.
Overall this is not the best practice in Spring but it solved my problem.
My problem is when iam trying to update an object with an embeddable class. If i watch the values, it seems the embeddable values are there. But the code throw an exception telling that that values are empty. And when i watch in the debugger, the object has de values but when i inspect the content in the exception, the object seems not having the values.
public abstract class ItemOrcamento extends EntidadeAuditavel {
#Embedded private ConfiguracaoColeta configuracaoColeta = new ConfiguracaoColeta();
}
And after the em.flush() it throws the exception InvalidStateException and when i search to look for the variables its appears this:
I use DbContext from custom assembly. LINQPad fails to run any query with this error message:
'project.model.Context' is inaccessible due to its protection level
Inconsistent accessibility: base class 'project.model.Context' is less accessible than class 'UserQuery'
Context class was marked as internal, but making it public didn't solved the issue.
public partial class Context : DbContext
{
public Context()
: base("name=Context")
{
}
// all constructors are public
Found this, but it didn't helped either.
Turned out I was referencing wrong DLL file.
Switching TypeAccess in EDMX to Public (or just editing generated files) had helped.
I am using JPA 2.1 with Hibernate 4.3.x on Glassfish 4, also tried the suggestion listed at https://coderwall.com/p/e5fxrw still get the below error. Could some one let me know what might be the issue ?
javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.dstar.entity.PurchaseOrder.idpurchaseorder
Below is the entity code, skipped getter and setter methods:
#Entity
#Table(name="purchaseorder")
#PersistenceUnit(name="dstarwarehouse",unitName="dstarwarehouse")
public class PurchaseOrder implements Serializable{
#Id #GeneratedValue(strategy=GenerationType.AUTO)
private int idpurchaseorder;
private boolean cash;
private boolean credit;
private String supplier;
private String orderedBy;
private String submittedBy;
private String approvedBy;
private Date expectedDate;
private Date creationDate;
private Date submittedDate;
private Date approvalDate;
private String purchaserName;
private double total;
#JoinColumn(name="idpurchaseorder", referencedColumnName="idpurchaseorder")
private List<Part> parts;
}
I had the same problem, using glassfish 4.1, hibernate 4.3.6, and injecting entity manager thru #PersistenceContext in a Stateless Session Bean, and saw some things interesting.
First, if I get the entity manager direct from the Persistence.createEntityManagerFactory("xxxxxx").createEntityManager() the problem disappear. Obviously, I don't like get things right in this way.
Change the server from glassfish 4.1 to glassfish 4, seems solve the problem too. So, at this moment, the problem looks like for me something wrong in glassfish 4.1.