Google App Engine Datanucleus enhencer - google-cloud-datastore

I have following class as superclass
#Entity
#MappedSuperclass
public class Location implements LocationCapable {
#Basic
private Double latitude;
#Basic
private Double longitude;
#Basic
private List<String> geocells;
#PrePersist
#Transient
private void generateGeoCells() {
geocells = GeocellManager.generateGeoCell(getLocation());
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
#Override
#Transient
#JsonIgnore
public Point getLocation() {
return new Point(latitude, longitude);
}
#Override
#Transient
#JsonIgnore
public String getKeyString() {
return latitude + ":" + longitude;
}
#Override
public List<String> getGeocells() {
return geocells;
}
public void setGeocells(List<String> geocells) {
this.geocells = geocells;
}
}
And another one which inherits from this
But when I try to run JUnit test I got this
Caused by: org.datanucleus.metadata.InvalidMetaDataException: Class Location has application-identity and no objectid-class specified yet has 0 primary key fields. Unable to use SingleFieldIdentity.
at org.datanucleus.metadata.AbstractClassMetaData.determineObjectIdClass(AbstractClassMetaData.java:1032)
at org.datanucleus.metadata.ClassMetaData.populate(ClassMetaData.java:205)
at org.datanucleus.metadata.AbstractClassMetaData.validateSuperClass(AbstractClassMetaData.java:720)
at org.datanucleus.metadata.AbstractClassMetaData.determineSuperClassName(AbstractClassMetaData.java:642)
at org.datanucleus.metadata.ClassMetaData.populate(ClassMetaData.java:193)
at org.datanucleus.metadata.MetaDataManager$1.run(MetaDataManager.java:2317)
at java.security.AccessController.doPrivileged(Native Method)
at org.datanucleus.metadata.MetaDataManager.populateAbstractClassMetaData(MetaDataManager.java:2311)
at org.datanucleus.metadata.MetaDataManager.populateFileMetaData(MetaDataManager.java:2148)
at org.datanucleus.metadata.MetaDataManager.initialiseFileMetaDataForUse(MetaDataManager.java:864)
at org.datanucleus.metadata.MetaDataManager.loadPersistenceUnit(MetaDataManager.java:794)
at org.datanucleus.jpa.EntityManagerFactoryImpl.initialisePMF(EntityManagerFactoryImpl.java:488)
at org.datanucleus.jpa.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:355)
at org.datanucleus.store.appengine.jpa.DatastoreEntityManagerFactory.<init>(DatastoreEntityManagerFactory.java:63)
at org.datanucleus.store.appengine.jpa.DatastorePersistenceProvider.createEntityManagerFactory(DatastorePersistenceProvider.java:35)
at javax.persistence.Persistence.createFactory(Persistence.java:172)
... 67 more
Also I've tried to add in supperclass the key field annotated with #Id but it gives no result for me

You have to have an #Id field, as the message says.

Looks like it was some problem with enhancing or eclipse plugin, no metter after restart of IDE the problem dessapiared. But only this problem with ID. Actually I've faced with another very strange problem related to embedded entities. I have following domain model:
#Entity
public class City {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Key id;
#Basic
private String name;
// Entity won't persist if location is not null, requires to persist with
// further update
#Embedded
// #OneToOne(cascade = CascadeType.ALL) it works the same when I add or remove this line
private Location location;
/* getters and setters */
}
#Embeddable
public class Location implements LocationCapable {
#Basic
#NotNull
private Double latitude;
#Basic
#NotNull
private Double longitude;
#Basic
private List<String> geocells;
/* getters and setters */
}
To test it I have following JUnit test case:
#Test
public void testSave() throws Exception {
City city = new City("testCity1", new Location(1d, 1d));
cityDao.persist(city);
assertNotNull(city.getId());
}
cityDao.persist(city) does simply jpaTemplate.persist(object);
And at when I try to persist this entity I got following exception:
Caused by: java.lang.IllegalArgumentException: out of field index :-1
at com.myproject.model.Location.jdoProvideField(Location.java)
at org.datanucleus.state.JDOStateManagerImpl.provideField(JDOStateManagerImpl.java:2585)
at org.datanucleus.state.JDOStateManagerImpl.provideField(JDOStateManagerImpl.java:2555)
at org.datanucleus.store.mapped.mapping.CollectionMapping.postInsert(CollectionMapping.java:91)
at org.datanucleus.store.mapped.mapping.EmbeddedPCMapping.postInsert(EmbeddedPCMapping.java:104)
at org.datanucleus.store.appengine.DatastoreRelationFieldManager.runPostInsertMappingCallbacks(DatastoreRelationFieldManager.java:217)
at org.datanucleus.store.appengine.DatastoreRelationFieldManager.access$200(DatastoreRelationFieldManager.java:48)
at org.datanucleus.store.appengine.DatastoreRelationFieldManager$1.apply(DatastoreRelationFieldManager.java:116)
at org.datanucleus.store.appengine.DatastoreRelationFieldManager.storeRelations(DatastoreRelationFieldManager.java:81)
at org.datanucleus.store.appengine.DatastoreFieldManager.storeRelations(DatastoreFieldManager.java:955)
at org.datanucleus.store.appengine.DatastorePersistenceHandler.storeRelations(DatastorePersistenceHandler.java:546)
at org.datanucleus.store.appengine.DatastorePersistenceHandler.insertPostProcess(DatastorePersistenceHandler.java:304)
at org.datanucleus.store.appengine.DatastorePersistenceHandler.insertObjects(DatastorePersistenceHandler.java:256)
at org.datanucleus.store.appengine.DatastorePersistenceHandler.insertObject(DatastorePersistenceHandler.java:240)
at org.datanucleus.state.JDOStateManagerImpl.internalMakePersistent(JDOStateManagerImpl.java:3185)
at org.datanucleus.state.JDOStateManagerImpl.flush(JDOStateManagerImpl.java:4513)
at org.datanucleus.ObjectManagerImpl.flushInternal(ObjectManagerImpl.java:2814)
at org.datanucleus.ObjectManagerImpl.flush(ObjectManagerImpl.java:2754)
at org.datanucleus.ObjectManagerImpl.preCommit(ObjectManagerImpl.java:2893)
at org.datanucleus.TransactionImpl.internalPreCommit(TransactionImpl.java:369)
at org.datanucleus.TransactionImpl.commit(TransactionImpl.java:256)
at org.datanucleus.jpa.EntityTransactionImpl.commit(EntityTransactionImpl.java:104)
at org.datanucleus.store.appengine.jpa.DatastoreEntityTransactionImpl.commit(DatastoreEntityTransactionImpl.java:55)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:467)
... 41 more

Related

Unable to perform LAZY FETCH operation using JPA in one-to-one relationship

I am trying to lazily fetch PetDetails entity using JPA. However, I get LazyInitialization Exception. I read many solutions for this exception and came to find solution using JOIN FETCH in JPQL. Also people recommended using Criteria queries. However, I am trying to look for a solution if there is a way I can fetch the PetDetails entity the way I want without using queries directly or depending on Criteria API or without using EAGER FETCH. I might be missing something. I would appreciate for any help or suggestion. Below are the code samples:
1. Controller class:
#Controller
public class PetController {
private static Logger LOGGER = Logger.getLogger(PetController.class);
#Autowired
private PetService petService;
#RequestMapping(value = "/", method = RequestMethod.GET)
public void manageAndDisplayPet() {
PetDetails petDetails = new PetDetails();
petDetails.setName("DOG");
Pet pet = new Pet(petDetails);
// save
petService.savePet(pet);
// retrieve
LOGGER.debug("**********************" + petService.getPet());
LOGGER.debug("**********************" + pet.getPetDetails());
}
}
2. PetService class:
#Service
public class PetService {
#Autowired
private PetDAO petDAO;
#Transactional
public void savePet(Pet pet) {
petDAO.savePet(pet);
}
#Transactional
public Pet getPet() {
return petDAO.getPet();
}
}
3. PetDAO class
#Repository
#EnableTransactionManagement
public class PetDAO {
#PersistenceContext(unitName = "petcontext")
private EntityManager entityManagerFactory;
public void savePet(Pet pet) {
entityManagerFactory.persist(pet);
}
public Pet getPet() {
Pet pet = (Pet) entityManagerFactory.find(Pet.class, 1);
return pet;
}
}
4. Pet Entity:
#Entity
#Table(name = "t_pet")
public class Pet {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#JoinColumn(name = "pet_details")
#OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private PetDetails petDetails;
public Pet() {
}
public Pet(PetDetails petDetails) {
this.petDetails = petDetails;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public PetDetails getPetDetails() {
return petDetails;
}
public void setPetDetails(PetDetails petDetails) {
this.petDetails = petDetails;
}
#Override
public String toString() {
return "Pet [id=" + id + ", petDetails=" + petDetails + "]";
}
}
5. PetDetails Entity:
#Entity
#Table(name = "pet_details")
public class PetDetails {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#Column(name = "pet_name")
private String name;
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;
}
#Override
public String toString() {
return "PetDetails [id=" + id + ", name=" + name + "]";
}
}
Thank you for your help.
Easy thing you can do is to call pet.getPetDetails() inside PetService#getPet. This solution is not very clear, but it will force JPA to fetch entity too. This is solution for your question, but not the good way anyways.
What is the good way?
The good way may depend on your particular usecase:
If you need this details everytime — you should use EAGER_FETCH
If you need it time to time than good solution it to use JPQL with JOIN FETCH
But the best way is to select not entites, but DTOs, which will contain whole information which your application needs and not more. It may be achieved with SELECT NEW expression as described here

Error While Fetching Data from Corda Custom Tables

How to fetch data from corda Custom tables?
my sample code is as follows :-
Api layer -- getIous() method
{
Field attributeValue=IOUSchemaV1.PersistentIOU.class.getDeclaredField("value");
CriteriaExpression currencyIndex = Builder.equal(attributeValue, "12");
QueryCriteria.VaultCustomQueryCriteria criteria = new
QueryCriteria.VaultCustomQueryCriteria(currencyIndex);
vaultStates = services.vaultQueryByCriteria(criteria,IOUState.class);
}
In ExamplePlugin I added below code for schema registration
public class ExamplePlugin extends CordaPluginRegistry implements
WebServerPluginRegistry
{
#NotNull
#Override
public Set<MappedSchema> getRequiredSchemas()
{
Set<MappedSchema> requiredSchemas = new HashSet<>();
requiredSchemas.add(new IOUSchemaV1());
return requiredSchemas;
}
}
My Schema classes are ---
public final class IOUSchema {
}
#CordaSerializable
public class IOUSchemaV1 extends MappedSchema
{
public IOUSchemaV1() {
super(IOUSchema.class, 1, ImmutableList.of(PersistentIOU.class));
}
#Entity
#Table(name = "iou_states")
public static class PersistentIOU extends PersistentState {
#Column(name = "sender_name") private final String senderName;
#Column(name = "recipient_name") private final String recipientName;
#Column(name = "value") private final int value;
public PersistentIOU(String senderName, String recipientName, int value) {
this.senderName = senderName;
this.recipientName = recipientName;
this.value = value;
}
public String getSenderName() {
return senderName;
}
public String getRecipientName() {
return recipientName;
}
public int getValue() {
return value;
}
}
}
my state has :-
public class IOUState implements LinearState, QueryableState
{
--- some code goes here and below methods as well.---
#Override
public PersistentState generateMappedObject(MappedSchema schema) {
if (schema instanceof IOUSchemaV1) {
return new IOUSchemaV1.PersistentIOU(
this.sender.getName().toString(),
this.recipient.getName().toString(),
this.iou.getValue());
} else {
throw new IllegalArgumentException("Unrecognised schema $schema");
}
}
#Override
public Iterable<MappedSchema> supportedSchemas() {
return ImmutableList.of(new IOUSchemaV1());
}
}
But all the time i am getting below exception.
Caused by: net.corda.core.node.services.VaultQueryException:
Please register the entity 'com.example.schema.IOUSchemaV1' class in your CorDapp's CordaPluginRegistry configuration (requiredSchemas attribute)
and ensure you have declared (in supportedSchemas()) and mapped (in generateMappedObject())
the schema in the associated contract state's QueryableState interface implementation.
Can anyone please help to resolve this.
Try deleting implements WebServerPluginRegistry from your plugin declaration.

How to access objects from event listener in javafx

I have an object Contract and it contains Summary and Observable List of another object ContractDetails inside it.
Now, I am using ContractDetails to populate in tableview from Contract object.
I have a save button, which on clicking needs to save Contract along with ContractDetails. I am able to access ContractDetails since they are in tableview.
How do I access Contract properties in eventlistener of save button.
The related code is given below
public class Contract {
private String tradeDate;
private String contractNote;
.....
.....
private String brokerId;
private ObservableList<ContractDetails> contractdetails = FXCollections.observableArrayList();
public Contract() {
}
public Contract(String tradeDate, String contractNote, ....., String brokerId,ObservableList<ContractDetails> contractdetails) {
this.tradeDate = tradeDate;
this.contractNote = contractNote;
....
....
this.contractdetails=contractdetails;
}
public String getTradeDate() {
return tradeDate;
}
public void setTradeDate(String tradeDate) {
this.tradeDate = tradeDate;
}
public String getContractNote() {
return contractNote;
}
public void setContractNote(String contractNote) {
this.contractNote = contractNote;
}
....
....
public ObservableList<ContractDetails> getContractdetails() {
return contractdetails;
}
public void setContractdetails(ObservableList<ContractDetails> contractdetails) {
this.contractdetails = contractdetails;
}
}
public class ContractDetails {
private String orderNo;
private String contractType;
private String symbol;
private String buysell;
private Integer quantity;
private Double buysellprice;
private Double netcontractValue;
public ContractDetails() {
}
public ContractDetails(String orderNo, String contractType, String symbol, String buysell, Integer quantity, Double buysellprice, Double netcontractValue) {
this.orderNo = orderNo;
this.symbol = symbol;
this.buysell = buysell;
this.quantity = quantity;
this.buysellprice = buysellprice;
this.netcontractValue = netcontractValue;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
....
....
public Double getNetcontractValue() {
return netcontractValue;
}
public void setNetcontractValue(Double netcontractValue) {
this.netcontractValue = netcontractValue;
}
}
In the controller
==================
public class ContractViewController implements Initializable {
#FXML
private TableView<ContractDetails> tblcontractfx;
#FXML
private TableColumn<ContractDetails, String> contractTypefx;
#FXML
private TableColumn<ContractDetails, String> symbolfx;
....
....
#FXML
private Button savefx;
#FXML
private TextField txtclientcodefx;
#FXML
private TextField txttradedtfx;
private void fetchContracts(TableView tableView, Contract contract)
{ txttradedtfx.setText(contract.getTradeDate());
txtclientcodefx.setText(contract.getClientCode());
symbolfx.setCellValueFactory(new PropertyValueFactory<ContractDetails, String>("symbol"));
contractTypefx.setCellValueFactory(new PropertyValueFactory<ContractDetails, String>("contractType"));
tableView.setItems((ObservableList) contract.getContractdetails());
#FXML
private void saveClicked(ActionEvent event) { DBConnection DBcon = new DBConnection();
//Now I am getting the contract details from tableview tblcontractfx
ObservableList<ContractDetails> contractdetails = tblcontractfx.getItems();
//How do I get the summary values from contract. I am able to get those which are in text fields like txttradedtfx and txtclientcodefx.However contractNote which I am not using, I still need to retrieve it to populate into database.
String clientCode=txtclientcodefx.getText();
Thanks
Just store the contract in a local variable.
Contract contract;
private void fetchContracts(TableView tableView, Contract contract)
{
this.contract = contract;
...
}
private void saveClicked(ActionEvent event) {
// here you have full access to the contract variable
String contractNote = contract.getContractNote();
}
As an alternative, if you insist on combining it all in a single table, you could put the Contract into the table via setUserData and retrieve it via getUserData.
By the way, I still don't get your code. Why is there a tableView parameter when you have full access to TableView<ContractDetails> tblcontractfx

keep records increment jpa-sqlite sqlite

My problem is when I save a person domiciled the id of the person record is 1 and the address 2 and when I save another record the id of person is 3 and the domicile 4 and so is increased, I want to increase as it should be is to record who is 1 and for of registered prox 1 for registration is 2 and 2 the next 3 and 3 and so on.
Then I leave the source code of the entities, the main, the method to keep and what gives me the console when I create the tables. I'm using netbeans 7.4 and EclipseLink library jpa2.1 and jdbc for sqlite dirver
#Entity
public class Persona implements Serializable {
protected static final long serialVersionUID = 1L;
#Id
#GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;
private String nombre;
#OneToOne (cascade = CascadeType.ALL)
private Domicilio domicilio;
public Persona() {
}
public Persona(String nombre) {
this.nombre = nombre;
domicilio= new Domicilio();
}
public Domicilio getDomicilio() {
return domicilio;
}
public void setDomicilio(Domicilio domicilio) {
this.domicilio = domicilio;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
Entity Domicilio:
#Entity
public class Domicilio implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String calle;
public Domicilio() {
}
public Domicilio(String calle) {
this.calle = calle;
}
public String getCalle() {
return calle;
}
public void setCalle(String calle) {
this.calle = calle;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
Method found in the class PersonaJpaController used to save the record
public void guardar(){
persona= new Persona("angel");
persona.getDomicilio().setCalle("alpatacal");
create(persona);
}
This is my main program
public class Pruebaentidades {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("pruebaentidadesPU");
EntityManager em = emf.createEntityManager();// create tables
PersonaJpaController p= new PersonaJpaController(Persistence.createEntityManagerFactory("pruebaentidadesPU"));
p.guardar();
}
}
This is what gives me the console when I create tables
[EL Info]: 2014-01-03 17:27:01.189--ServerSession(24979675)--EclipseLink, version: Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5
[EL Info]: connection: 2014-01-03 17:27:01.379--Not able to detect platform for vendor name [SQLite3]. Defaulting to [org.eclipse.persistence.platform.database.DatabasePlatform]. The database dialect used may not match with the database you are using. Please explicitly provide a platform using property eclipselink.platform.class.name.
[EL Info]: connection: 2014-01-03 17:27:01.446--ServerSession(24979675)--file:/C:/Users/VM/Desktop/Mis Proyectos/Proyectos Netbeans/pruebaentidades/build/classes/_pruebaentidadesPU login successful
[EL Warning]: 2014-01-03 17:27:01.6--ServerSession(24979675)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: near "CONSTRAINT": syntax error
Error Code: 0
Call: ALTER TABLE PERSONA ADD CONSTRAINT FK_PERSONA_DOMICILIO_ID FOREIGN KEY (DOMICILIO_ID) REFERENCES DOMICILIO (ID)
Query: DataModifyQuery(sql="ALTER TABLE PERSONA ADD CONSTRAINT FK_PERSONA_DOMICILIO_ID FOREIGN KEY (DOMICILIO_ID) REFERENCES DOMICILIO (ID)")
for those who have this same problem I want to say that the solution to the problem was to create a sequence for each table and use the strategy generation secuence.
add these statements for each entity allowing me to create a sequence for each table
Entity Persona:
#GeneratedValue (strategy = GenerationType.SEQUENCE, generator = "PERS_SEQ")
#SequenceGenerator ( name = "PERS_SEQ" , sequenceName = "PERS_SEQ")
Entity Domicilio:
#GeneratedValue(strategy = GenerationType.SEQUENCE , generator = "DOM_SEQ")
#SequenceGenerator ( name = "DOM_SEQ" , sequenceName = "DOM_SEQ")

No property show found for type in spring data

I have simple webapp in spring 3, jpa, eclipse link, spring data. When I try run it (on VMware server) I have got error:
org.springframework.data.mapping.PropertyReferenceException: No property show found for type foo.domain.Catalog
Entity:
#Entity
#Table(name="catalog")
#NamedQuery(name="Catalog.findAll", query="SELECT c FROM Catalog c")
public class Catalog implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private String id;
private String adres;
private String opis;
private String tytul;
//bi-directional many-to-one association to Category
#ManyToOne
#JoinColumn(name="cname", referencedColumnName="name")
private Category category;
public Catalog() {
}
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public String getAdres() {
return this.adres;
}
public void setAdres(String adres) {
this.adres = adres;
}
public String getOpis() {
return this.opis;
}
public void setOpis(String opis) {
this.opis = opis;
}
public String getTytul() {
return this.tytul;
}
public void setTytul(String tytul) {
this.tytul = tytul;
}
public Category getCategory() {
return this.category;
}
public void setCategory(Category category) {
this.category = category;
}
Interface repository:
public interface KatalogRepository extends JpaRepository<Catalog, Long>{
#Query("select c from Catalog c left join fetch c.name")
public List<Catalog> showAll();
#Query("select c from Catalog c where c.tytul like %?1")
public Catalog findByTytul(String tytul);
#Query("select c from Category c")
public List<Category> showAllCategory();
#Query("select c from Category c where c.id =:id")
public Category findCategoryById(Long id);
}
I tried many solutions but didn't help. Has someone got the similar problem? Thanks for help.
My dao class:
#Repository
public class KatalogDAO {
#Autowired
KatalogRepository katalogRepository;
//#Autowired
//CategoryRepository categoryRepository;
#Transactional
public List<Catalog> showAllSites(){
return katalogRepository.showAll();
}
#Transactional
public void saveSite(Catalog catalog){
katalogRepository.saveAndFlush(catalog);
}
#Transactional
public Catalog showByTitle(String tytul){
return katalogRepository.findByTytul(tytul);
}
#Transactional
public List<Category> showAllCategory(){
return katalogRepository.showAllCategory();
}
#Transactional
public Category findCategoryById(Long id){
return katalogRepository.findCategoryById(id);
}
}
And endpoint:
#Component
public class CatalogEndpoint {
#Autowired
KatalogDAO katalogDAO;
public List<Catalog> showAllSites(){
return katalogDAO.showAllSites();
}
public void saveSite(Catalog catalog ){
katalogDAO.saveSite(catalog);
}
public Catalog getByTitle(String tytul){
return katalogDAO.showByTitle(tytul);
}
public List<Category> showCategory(){
return katalogDAO.showAllCategory();
}
public Category findCategoryById(Long id){
return katalogDAO.findCategoryById(id);
}
}
I think the problem is that you wrote this query :
#Query("select c from Category c")
Which should be :
#Query("select * from Category c")
The error is probably caused by the Spring Data JPA framework, because if you give it a wrong request, it will probably try to generate a request based on the method name, but in this case, your method name should be "findAll". Moreover, the findAll() method is already given by the Spring Data JPA framework thanks to the CRUDRepository interface.

Resources