JPQL JPA access google datastore - java

My entity class :
public class ACCOUNT implements Serializable{
private static final long serialVersionUID = 1L;
#Id
#Column(name = "USERNAME")
private String Username;
#Column(name = "PASSWORD")
private String Password;
public ACCOUNT(String user,String pass)
{
this.Username=user;
this.Password=pass;
}
// geter and setter
my persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="transactions-optional">
<provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
<class>com.materialshop.server.Datastore.ACCOUNT</class>
<exclude-unlisted-classes/>
<properties>
<property name="datanucleus.NontransactionalRead" value="true"/>
<property name="datanucleus.NontransactionalWrite" value="true"/>
<property name="datanucleus.nontx.atomic" value="true"/>
<property name="datanucleus.ConnectionURL" value="appengine"/>
</properties>
</persistence-unit>
</persistence>
Now persist account and it was successful
ACCOUNT ac=new ACCOUNT("admin","123");
em.persist(ac);
I checked
http://localhost:8888/_ah/admin
and yes ,there is ACOUNT entity with 1 record admin and 123
but when I use JPQL to get all entity , it returned null
Query q=new Query("SELECT ac FROM ACCOUNT ac");
List<ACCOUNT> list=q.getResultList();
even with
em.find(ACCOUNT.class,"admin");
return null too
Did i miss something ? Please help me , very grateful for your help

Related

hibernate gen table error org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build()" because "entityManagerFactoryBuilder" is null

I fail to generate table from entity in eclipse with error org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build()" because "entityManagerFactoryBuilder" is null
I have three hibernate classes :
#Entity
#Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
abstract public class GameBean implements Serializable {
private static final long serialVersionUID = -8690022943103849312L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
protected GameBean(){
}
}
#Entity
#Table(name="CheckersGame")
#PrimaryKeyJoinColumn(name="id")
public class CheckersGameBean extends GameBean{
/**
*
*/
private static final long serialVersionUID = -6448336204266253962L;
protected CheckersGameBean(){
super();
}
}
#Entity
#Table(name="ChessGame")
#PrimaryKeyJoinColumn(name="id")
public class ChessGameBean extends GameBean {
/**
*
*/
private static final long serialVersionUID = 3921250444757041509L;
protected ChessGameBean(){
super();
}
}
antlr-2.7.7.jar
byte-buddy-1.10.10.jar
classmate-1.5.1.jar
commons-beanutils-1.9.3.jar
commons-collections.jar
commons-dbcp-1.4.jar
commons-lang.jar
commons-logging-1.1.1.jar
dom4j-2.1.3.jar
ezmorph.jar
FastInfoset-1.2.15.jar
hibernate-commons-annotations-5.1.0.Final.jar
hibernate-core-5.4.15.Final.jar
hibernate-envers-5.4.15.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
hibernate-jpamodelgen-5.4.15.Final.jar
istack-commons-runtime-3.0.7.jar
jandex-2.1.3.Final.jar
javassist-3.24.0-GA.jar
javax.activation_1.1.0.v201211130549.jar
javax.activation-api-1.2.0.jar
javax.persistence-api-2.2.jar
javax.xml_1.3.4.v201005080400.jar
javax.xml.bind_2.2.0.v201105210648.jar
jaxb-api-2.3.1.jar
jaxb-runtime-2.3.1.jar
jboss-logging-3.3.2.Final.jar
jboss-transaction-api_1.2_spec-1.1.1.Final.jar
json-lib-2.2.2-jdk15.jar
log4j.properties
mysql-connector-java-8.0.30.jar
stax-ex-1.8.jar
txw2-2.3.1.jar
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="JPAUNIT-BOARDGAMES" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider
<class>com.strategisina.ontheboard.bean.CheckersGameBean</class>
<class>com.strategisina.ontheboard.bean.ChessGameBean</class>
<class>com.strategisina.ontheboard.bean.GameBean</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/DATABASE"/>
<property name="javax.persistence.jdbc.user" value="USER"/>
<property name="javax.persistence.jdbc.password" value="PASSWORD"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>
When I try to generate the tables with the JPA Tool/Generate Tables from Entries, the above error appears and no generation is done.
I would be really cool to get help with that, its a little time I'm on it !
I succeed in making it work. I used in Console Configuration a Annotation (Jdk 1.5+) type and a Hibernate Version 4.3. And then I succeeded in generating the tables with this console configuration. As I didn't understand what was wrong, I can't explain the solution. It works as well with JPA, with persistence.xml as follows
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="JPAUNIT" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>```

Java EE/ JPA #PersistenceContext(unitName = ) doesn't inject Hibernate entity manager implementation

Currently, I learn the Java EE JPA specification. I am using Hibernate as JPA implementation. And I have the next issue:
I have a simple entity
#Entity
public class Book {
#Id
#GeneratedValue
private Long id;
private String title;
private String description;
private Float unitCost;
public Book() {
}
// getters and setters
}
And a simple Service:
public class BookService {
#PersistenceUnit(unitName = "book_store")
private EntityManager entityManager;
public void create(Book book) {
entityManager.persist(book);
entityManager.flush();
}
}
The persistence.xml has next view:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
version="2.2">
<persistence-unit name="book_store">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.example.jpatest1.Book</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password"
value="somePasss"/>
<property name="javax.persistence.jdbc.user" value="sammy"/>
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/book_store?
autoReconnect=true&useSSL=false"/>
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
And I don't understand why the Hibernate Session don't inject into BookService. In book service as EntityManager I got org.jboss.as.TransactionScopedEntityManager instead of SessionImpl from Hibernate. Why using #PersistenceContext(unitName = ) I don't get Hibernate EntityManager implementation?
Also, I pin link on github: link on code source
The type of the object field entityManager is EntityManager, so you get injected what you declared in that field.
To get session from EntityManager, you can try this:
Session session = entityManager.unwrap(Session.class);

org.hibernate.hql.internal.ast.QuerySyntaxException: transaction is not mapped

I have an error like this : java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Transaction is not mapped [SELECT NEW model.DailyReport(SUM(c.amount), date(c.transactionTimestamp)) FROM transaction AS c GROUP BY date(c.transactionTimestamp)]
and here is my code :
String queryStr = "SELECT NEW model.DailyReport(SUM(c.amount), date(c.transactionTimestamp)) FROM Transaction AS c GROUP BY date(c.transactionTimestamp)";
TypedQuery<Transaction> query = entityManager.createQuery(queryStr, Transaction.class);
query.setFirstResult(offset);
query.setMaxResults(limit);
return query.getResultList();
Help me, BTW I'm new in JPA. Thx!
Edited :
Here is my Transaction Class :
#Table(name = "Transaction")
#Entity
public class Transaction implements Serializable{
#Id // ID
#Column(length = 32, nullable = false)
private String id;
#Column(length = 32, nullable = false)
private String srcAccountId;
#Column(length = 32, nullable = false)
private String dstAccountId;
private Double amount;
#JsonFormat(pattern = Json.JSON_FORMAT_TIMESTAMP)
#Temporal(TemporalType.TIMESTAMP)
private Date transactionTimestamp;
// Setter and Getter here
}
And here is my persistence.xml :
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="RestNeo-1" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/RestNeoDS</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
Thank you.

Hibernate : All tables in SQL Server database are not auto-created

create tables from Java in database on Microsoft SQL Server 2012. All tables are created, except one table. I'm using JPA and there is my persistence.xml :
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="teknikPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/teknikNDataSource</jta-data-source>
<class>com.royken.entities.Bloc</class>
<class>com.royken.entities.Elements</class>
<class>com.royken.entities.Organes</class>
<class>com.royken.entities.SousOrganes</class>
<class>com.royken.entities.Utilisateurs</class>
<class>com.royken.entities.Zone</class>
<class>com.royken.entities.Reponse</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
<property name="eclipselink.logging.level" value="OFF"/>
<property name="eclipselink.cache.shared.default" value="false"/>
<property name="eclipselink.query-results-cache" value="false"/>
<!-- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.engine.transaction.jta.platform.internal.SunOneJtaPlatform" />
<property name="hibernate.transaction.factory_class" value="org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.classloading.use_current_tccl_as_parent" value="false"/>-->
<!--<property name="javax.persistence.schema-generation.database.action" value="create"/> -->
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
This is how I define my classes :
#Entity
#XmlRootElement(name = "elements")
#Table(name = "ELEMENTS")
#XmlAccessorType(XmlAccessType.FIELD)
public class Elements implements Serializable {
private static final long serialVersionUID = 1L;
#OneToMany(mappedBy = "elements")
private List<Reponse> reponses;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID")
private Long id;
#Version
#Column(name = "VERSION")
private int version;
#Column(name = "NOM")
private String nom;
#Column(columnDefinition = "tinyint(1) default true", name = "HASBORNS")
private boolean hasBorns;
#Column(columnDefinition = "tinyint(1) default true", name = "CRITERIAALPHA")
private boolean criteriaAlpha;
}
I have defined 7 tables like that, but only 6 tables are created, Elements tables is not created. When I change the datasource by using a mysql database (without changing any part of code), all my tables are well created.
What can be the issue ?
The image bellow shows the result in SQL server, Elements table is not present.
In your persistence.xml Use :
<property name="eclipselink.deploy-on-startup" value="true" />
In your code, you may use:
import javax.ejb.Stateless;
import entity.userEntity;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
Look for EntityManager
#PersistenceContext
private EntityManager entityManager;
and then use it like :
Query query = entityManager.createQuery("SELECT e FROM Elements e WHERE e.id= :idValue");
query.setParameter("idValue", 1);
Elements elements = null;
try {
elements = (Elements) query.getSingleResult();
} catch (NoResultException ex) {
ex.printStackTrace();
}
You may refer to this
If that too doesn't help look here
I found the solution to my problem. Entity table was not created because SQL Server does not accept true as default value for hasborns and criteriaalpha. Also, it does not now the size to allocate to tinyint type. So it throws an error during table creation. To solve this issue, I replaced:
#Column(columnDefinition = "tinyint(1) default true", name = "HASBORNS")
private boolean hasBorns;
#Column(columnDefinition = "tinyint(1) default true", name = "CRITERIAALPHA")
private boolean criteriaAlpha;
with:
#Column(columnDefinition = "BIT default 1", name = "HASBORNS", length = 1)
private boolean hasBorns ;
#Column(columnDefinition = "BIT default 1", name = "CRITERIAALPHA", length = 1)
private boolean criteriaAlpha ;
And it worked

Hibernate/H2 #OneToMany "Referential integrity constraint violation" on remove of child?

Hello I have a rather simple relationship.
A Patient can have several Measurements. I used a H2 file database and Hibernate.
The Problem:
When I try to remove a Measurement from a Patient I get a the following Error.
Referential integrity constraint violation: "FK_O423U89KR6K78NNG1PO0ISDF6: PUBLIC.PATIENTVO_MEASUREMENTVO FOREIGN KEY(MEASUREMENTS_ID) REFERENCES PUBLIC.MEASUREMENTVO(ID) (X'3c2b9ee868f645c5a5a743c2a409ab5e')"; SQL statement:
delete from MeasurementVO where id=? [23503-185]
The Patient part of the Relationship:
#OneToMany(orphanRemoval=true, cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE}, mappedBy = "patient", fetch = FetchType.EAGER)
public List<MeasurementVO> getMeasurements() {
return measurements;
}
The Measurement Part of the Relationship:
#ManyToOne
#JoinColumn
public PatientVO getPatient() {
return patient.get();
}
The function which should delete a Measurement:
#Override
#Transactional
public boolean removeMeasurementFromPatient(PatientVO patientVO, MeasurementVO measurementVO) {
EntityManager manager = emProvider.get();
PatientVO patientAlreadyInDB = manager.find(PatientVO.class, patientVO.getPatientId());
if (patientAlreadyInDB != null) {
patientAlreadyInDB.getMeasurements().removeIf(measurement -> measurement.getId().equals(measurementVO.getId()));
manager.merge(patientAlreadyInDB);
return true;
} else {
return false;
}
}
And the Persistence XML:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="db" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>ch.fhnw.ima.doggait.vo.PatientVO</class>
<class>ch.fhnw.ima.doggait.vo.MeasurementVO</class>
<class>ch.fhnw.ima.doggait.vo.MeasurementAttributesVO</class>
<class>ch.fhnw.ima.doggait.vo.TimeMark</class>
<properties>
<property name="connection.driver_class" value="org.h2.Driver"/>
<!-- <property name="hibernate.connection.url" value="jdbc:h2:mem:db"/>-->
<property name="hibernate.connection.url" value="jdbc:h2:file:./database/dogdatabase"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<!--<property name="hibernate.hbm2ddl.auto" value="create-drop"/>-->
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
Does somebody has a hint why I am not able to remove a Measurement from a patient and then persist him/her?
From the name of the table whose constraint is being violated (PATIENTVO_MEASUREMENTVO), seems like you have an intermediate table to implement the relation. This is normally used in many to many relationships.
Being a one to many relationship why not just adding a foreign key from the Measurement to Patient?

Categories