When I generate a table with field LocalDate, it will be mandatory.
#Column(name = "supported_date", nullable = false)
private LocalDate supportedDate;
I don't understand this rule.
It's right there in the #Column annotation: nullable = false means that the column is not nullable, which means that it cannot contain null values, which means that the column is required.
Related
Having this Entity
#Table(keyspace = KEYSPACE)
public class CE_TimeSeries extends Entity implements TimeSeriesPoint{
#PartitionKey(1)
private String typeId;
#ClusteringColumn(value=1, asc=true)
private String userId;
#ClusteringColumn(value=2, asc=true)
private Date startDate;
#Column
private Date endDate;
#Column
private int groupInterval;
#Column
private int interval;
}
This CQL
SELECT startDate, endDate, groupInterval, interval FROM CE_TimeSeries WHERE typeId
= :typeId and userId = :userId and ( endDate >= :fromDate or ( startDate >=
:fromDate and startDate <= :toDate ) )
Give the Exception :
Caused by: com.datastax.driver.core.exceptions.SyntaxError: line 1:142
mismatched input 'or' expecting ')' (... ( endDate >= :fromDate [or] (...)
While I don't actually see a question here, I'll assume that you are wondering why you are getting an exception. There are two things wrong with your query.
CQL does not allow the use of OR in the WHERE clause.
CQL does not allow parens in the WHERE clause. Plus, not having OR available kind of precludes the need for parens.
The bottom line, is that CQL is not SQL, and the logic you can apply in the WHERE clause is largely dependent on the storage model.
I have problem when create table with UUID type field using CassandraAdminOperations.createTable. I defined field in table with type uuid, but when the table was created using CassandraAdminOperations.createTable, the field was created as timeuuid. Is there any way to force the field to uuid instead of timeuuid ?
Here is the field definition in class,
#PrimaryKeyColumn(name = "id", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
private UUID id;
#Column(value = "url")
private String url;
But when call CassandraAdminOperations.createTable to create the table, the log shows the following and the id filed was created as timeuuid,
00:43:23.877 [localhost-startStop-1] DEBUG o.s.d.c.core.CassandraAdminTemplate - CREATE TABLE IF NOT EXISTS id_to_url_map (id timeuuid, url text, PRIMARY KEY (id));
Thanks!
I had the same problem. But after some investigation, I found that you can add #CassandraType annotation, and explicitly specify the class.
import com.datastax.driver.core.DataType;
#PrimaryKeyColumn(name = "id", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
#CassandraType(type = DataType.Name.UUID)
private UUID id;
When using the Update method on ODSController (ie using GridView / FormView in an ASP.NET application) and passing null values to nullable columns, the nullable column value remains unchanged.
This is due to the way that update method instantiate the ActiveRecord to update:
public void Update(Guid MyKey, ...)
{
MyItem item = new MyItem();
item.MarkOld();
item.IsLoaded = true;
...
item.Save(UserName);
}
By creating an empty MyItem instance (all fields are null) and setting a nullable field to null doesn' t allow the column to finish in the DirtyColumns collection (see ActiveHelper GetUpdateCommand).
Am I going to have write custom update functions for EVERY table in my database to resolve this?
I tried to get all MainIntervals in a certain date interval. But I get always null.
Here is the JPA Entity
#Table(name="MAIN_INTERVAL")
#NamedQueries({
#NamedQuery(name = MainInterval.FIND_ALL, query = " select m from MainInterval m"),
#NamedQuery(name = MainInterval.FIND_BETWEEN,
query = "select m from MainInterval m where m.mainIntervalStart
BETWEEN :startDate AND :endDate
AND m.mainIntervalEnd BETWEEN :startDate AND :endDate"
) })
public class MainInterval implements Serializable {
public static final String FIND_ALL = "MainInterval.findAll";
public static final String FIND_BETWEEN = "MainInterval.findBetween";
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name="MAIN_INTERVAL_ID")
private Long id;
#Column(name="MAIN_INTERVAL_START")
#Temporal(javax.persistence.TemporalType.DATE)
private Date mainIntervalStart;
#Column(name="MAIN_INTERVAL_END")
#Temporal(javax.persistence.TemporalType.DATE)
private Date mainIntervalEnd; }
And in the EJB SessionBeans I have the method:
public List<MainInterval> findMainIntervalsBetween(Date startDate, Date endDate){
List<MainInterval> resultList = em.createNamedQuery(MainInterval.FIND_BETWEEN, MainInterval.class)
.setParameter("startDate", startDate, TemporalType.DATE).setParameter("endDate", endDate, TemporalType.DATE).getResultList();
return resultList;
}
But when I call it from JSF with CDI the resultList is always null. Although I have some MainIntervals that meet the conditions between startDate and endDate.
I would be very grateful to every answer or links for tutorial.
Best regards!
Your query is:
select m from MainInterval m where m.mainIntervalStart
BETWEEN :startDate AND :endDate
AND m.mainIntervalEnd BETWEEN :startDate AND :endDate
Your first row's interval is [01.05.2012, 31.05.2012], and your second row's interval is [01.05.2012, 01.08.2012]. The arguments of the query are 10.05.2012, 20.05.2012.
So, for your first row:
m.mainIntervalStart BETWEEN :startDate AND :endDate
01.05.2012 BETWEEN 10.05.2012 AND 20.05.2012 : false
So the first row is not returned.
For your second row:
m.mainIntervalStart BETWEEN :startDate AND :endDate
01.05.2012 BETWEEN 10.05.2012 AND 20.05.2012 : false
So the second row isn't returned either.
Everything looks normal to me.
I want to do this query with metamodel but I can't!! I dont know how to do this.
MYSQL QUERY (with this query I want to get all the rows from the Clases table that are teaching in this moment):
SELECT * FROM clases cl
WHERE CURRENT_TIME() BETWEEN
(SELECT ml2.inicio FROM modulos ml2 WHERE cl.modulo_id=ml2.modulo_id ) AND
(SELECT ml2.fin FROM modulos ml2 WHERE cl.modulo_id=ml2.modulo_id) AND
cl.fecha=CURRENT_DATE();
These are my entities:
ENTITY MODULOS
#Entity
#Table(name = "modulos")
public class Modulos implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "modulo_id")
private Integer moduloId;
#Basic(optional = false)
#Column(name = "inicio")
#Temporal(TemporalType.TIME)
private Date inicio;
#Basic(optional = false)
#Column(name = "fin")
#Temporal(TemporalType.TIME)
private Date fin;
#Basic(optional = false)
#Column(name = "modulo")
private String modulo;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "modulos")
private List<GruposHorarioHasModulos> gruposHorarioHasModulosList;
//getters and setters...
}
ENTITY CLASES
#Entity
#Table(name = "clases")
public class Clases implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "clase_id")
private Integer claseId;
#Basic(optional = false)
#Column(name = "aula")
private String aula;
#Basic(optional = false)
#Column(name = "fusion")
private boolean fusion = false;
#Basic(optional = false)
#Column(name = "clase_numero")
private Integer claseNumero;
#Basic(optional = false)
#Column(name = "clase_impartida")
private boolean claseImpartida = false;
#JoinColumn(name = "modulo_id", referencedColumnName = "modulo_id")
#ManyToOne(optional = false)
private Modulos modulos;
//getters and setters...
}
I have this:
EntityManager em1 = Persistence.createEntityManagerFactory("myPU").createEntityManager();
CriteriaBuilder cb = em1.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery();
Root<Clases> root = cq.from(Clases.class);
cq.select(root.get(Clases_.claseId));
Subquery<Date> sqOne = cq.subquery(Date.class);
Root<Modulos> root2 = sqOne.from(Modulos.class);
sqOne.select(root2.get(Modulos_.inicio));
Subquery<Date> horaInicio = sqOne.select(root2.get(Modulos_.inicio)).where(
cb.equal(
root2.get(Modulos_.moduloId),
root.get(Clases_.modulos).get(Modulos_.moduloId)
)
);
Subquery<Date> sqTwo = cq.subquery(Date.class);
Root<Modulos> root3 = sqTwo.from(Modulos.class);
Subquery<Date> horaFin = sqTwo.select(root3.get(Modulos_.fin)).where(
cb.equal(
root3.get(Modulos_.moduloId),
root.get(Clases_.modulos).get(Modulos_.moduloId)
)
);
cq.where(cb.between(cb.currentTime(), horaInicio, horaFin));
em1.createQuery(cq).getResultList();
This code gives me the following exception:
Exception in thread "main" java.lang.ClassCastException:
org.eclipse.persistence.internal.jpa.querydef.SubQueryImpl cannot be
cast to org.eclipse.persistence.internal.jpa.querydef.ExpressionImpl
If i change the where clause for this one...
cq.where(cb.between(cb.currentTime(), new Date(), new Date()));
... it works but without my Subquery, then i can see the error came from my subquery, but I don't know why, if I change the where clause for this one...
cq.where(cb.greaterThan(cb.currentTime(), horaInicio));
... I get this:
SELECT t0.clase_id FROM clases t0, clases t1 WHERE (CURRENT_TIME > (SELECT t2.inicio FROM modulos t3, modulos t2 WHERE ((t2.modulo_id = t3.modulo_id) AND (t3.modulo_id = t0.modulo_id))))
I can see the problem are the 2 subqueries in the between clause.
Please I need help with this, I spent 2 weeks looking for an answer but... nothing... help.
I'm using JPA 2.0 with Netbeans and EclipseLink with metamodel generator and Java 6.
I want to do it with metamodels and criteriasbuilder and criteriasquerys
As you can see i need to do a subquery in the where clause and in that where clause I need to do a between where each parameter have a subquery, like this:
SELECT * FROM X WHERE CURRENT_TIME BETWEEN **MY_SUBQUERY_ONE** AND **MY_SUBQUERY_TWO**
Abit late answer but..
Subquery<Date> sqOne = cq.subquery(Date.class);
Root<Modulos> root2 = sqOne.from(Modulos.class);
sqOne.select(root2.get(Modulos_.inicio));
Subquery<Date> horaInicio = sqOne.select(root2.get(Modulos_.inicio)).where(
cb.equal(
root2.get(Modulos_.moduloId),
root.get(Clases_.modulos).get(Modulos_.moduloId)
)
);
Not sure what you are trying to do here, and it might or might not be related to your error. You create sqOne and make a selection. Then you redo the selection, replacing the previous one and copy the result to horaInicio. What is the purpose of this? You might aswell skip sqOne.select(root2.get(Modulos_.inicio)); and keep using sqOne instead of horaInicio.
Also, I'm not sure you can mix and match Roots created from different queries.