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.
Related
I would like to annotate a method with Spring Data #Id but it only works with fields, despite the fact that the annotation can be used on methods.
Is there a way to use #Id on methods too?
I'm using Spring Boot 1.3.0.RELEASE
EDIT
Actually I have this interface that will have an instance being created at runtime.
import org.springframework.data.annotation.Id;
#Document(indexName = "index", type = "document")
public interface Document {
#Id
Integer getId();
}
And this repository.
public interface DocumentRepository extends ElasticsearchCrudRepository<Document, Integer> {
}
Problem is that SimpleElasticsearchPersistentProperty from spring-data-elasticsearch 1.3.0.RELEASE always look for fields:
https://github.com/spring-projects/spring-data-elasticsearch/blob/1.3.0.RELEASE/src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentProperty.java
That way if I create an asbtract class instead and put #Id on a field, everything works fine.
The #Id annotation does work on properties, i.e. you can put it on getters, setters or fields. If this does not work something is wrong. Possible reasons are:
the names don't fit the property conventions
you are using the wrong #Id annotation
It does not work on arbitrary methods because Spring Data wouldn't be able to determine a name for that non-property, which in turn is required for many features.
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.
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?
This might be a simple question, but I am not an expert with Spring.
I have a class Message that represents documents in a collection called "messages" in mongoDB.
#Document(collection = "archive")
public class Message{
#Id
private String messageId;
private String from;
private String to;
// more stuff
}
The interface MessagesRepository extends MongoRepository<Message,String>:
public interface MessagesRepository extends MongoRepository<Message, String>{
}
I have MongoRepository.findOne(ID) method calls where ID is a string, across different modules in my code.
It all works fine. The ID, called messageId is a String. But I want to insert it into Mongo in binary encoded format. My first rough guess was to do
#Id
private byte[] messageId;
in the Message class
But it neither felt right to me, nor worked because I found out I was getting different byte[] for different String objects even with the same values.
I've seen MongoMappingConverter, but not sure if it works. Can someone throw some light?
Thanks a ton!
I found no way to do that automatically as of now. However, to achieve this I've only changed the messageId from String to byte[].
That seems to have worked.
And correction : the find() method of Spring-Data doesn't work with a byte[] but Mongo's java driver works so I'm going to adjust the Spring-Data methods.
I would like to define a JPA persisted property in a Groovy Mixin and then use it in several entity classes. I couldn't get this to work with JPA annotations and Hibernate - has anyone been successful with this combination?
I have a set up an example Maven project which shows what I'm trying to do and a single JUnit test which defines the behavior I would like.
https://github.com/gilday/groovy-mixin-jpa-test
Briefly:
#Category(Person) class HasPreferences {
#ElementCollection
final Collection<Preference> preferences = []
}
#Entity
#Mixin(HasPreferences)
class Person {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
long id
String name
}
Since #Mixin is dynamic, i doubt JPA will be able to find your mixed properties. I think you need some compile-time code generation, like #Delegate. Even so, JPA will try to persist the generated property. There is a discussion in groovy mailing list concerning the creation of a #Trait annotation which might be what you want.