In a parent Entity, is there anyway to set a column's ColumnAttribute.Expression string pointing at the referenced EntitySet<ChildEntity> Count or is the Expression only computed from columns in the same Entity?
EDIT:
[Column(Expression = "ChildEntitySet.Count")] throws an exception.
[Column(Expression = "COUNT(ChildEntitySet)")] throws an exception too.
Related
I'm trying to map my entity to projection using the below query but i'm getting error as
Exception : could not extract ResultSet SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
here is the query
#Query("select rf.rfqID as rfqID,rf.creationDate as creationDate," +
"rf.deadLineDate as deadLineDate,rf.details as details," +
"rf.message as message, rf.rfqDoc as rfqDoc," +
"CASE WHEN (rf.creationDate > CURRENT_DATE) THEN 'open' ELSE 'closed' END as status," +
"rf.rfqMembers as rfqMembers " +
"from RFQ rf where rf.createdBy = ?1")
Page<RfqDto> loadAllRfq(String creator, Pageable pageable);
In my Dto I have an extra status column which I don't want to persist in db and would like to get the status via query
here is my projection interface
public interface RfqDto {
String rfqID();
Date creationDate();
Date deadLineDate();
String details();
String message();
String rfqDoc();
String status();
List<RfqMember> rfqMembers();
}
The root cause of your problem is here:
In my Dto I have an extra status column which I don't want to persist in db and would like to get the status via query
As it's explained in the documentation:
The important bit here is that the properties defined here exactly match properties in the aggregate root.
...
The query execution engine creates proxy instances of that interface at runtime for each element returned and forwards calls to the exposed methods to the target object.
So, you can not use spring data jpa projection for your case. You can not use hibernate/jpa projection as well, because it dose not support collections in row results.
You can try to use Blaze-Persistence Entity Views. See for example this answer.
I am working on a project with Spring boot. I have a problem with Spring JPA Data. I want to check if a record already exists in the db using two parameters
#Transactional
#Modifying
#Query("SELECT CASE WHEN COUNT(dfe) > 0 THEN 'true' ELSE 'false' END FROM DeployedFunctionEntity dfe WHERE dfe.function.idFunction = ?1 and dfe.gatewayId = ?2")
boolean existsByFunctionIdAndGatewayId(#Param("idFunction") Integer functionId,
#Param("gatewayId") Integer gatewayId);
boolean exist = deployedFunctionDao.existsByFunctionIdAndGatewayId(functionId, gatewayId);
I always get the following error:
org.springframework.dao.InvalidDataAccessApiUsageException: Modifying queries can only use void or int/Integer as return type!;
The nested exception is
java.lang.IllegalArgumentException: Modifying queries can only use void or int/Integer as return type!
How can I fix it?
The #Modifying has no use for non-modifying queries, thus, must only be used for INSERT, UPDATE, DELETE or DDL queries. Since you are only making a SELECT query, remove the annotation and it'll work without problems.
I have following Linq code
// query = IQueryable<DataClass>
query = query.Where(m => m.Column1.Contains(model.search.value)
|| m.DateTimeColumn2.ToString("dd.MM.yyyy").StartsWith(model.search.value));
return query.ToList() // here the error is thrown
I get NullReferenceException error
Exception has occurred: CLR/System.NullReferenceException An exception
of type 'System.NullReferenceException' occurred in
Microsoft.EntityFrameworkCore.dll but was not handled in user code:
'Object reference not set to an instance of an object.' at
System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext()
at
Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor1.EnumeratorExceptionInterceptor.MoveNext()
if i commented out the line for 2nd column it works
//|| m.DateTimeColumn2.ToString("dd.MM.yyyy").StartsWith(model.search.value)
model.search.value is string value I am trying to filter all columns. The DateTimeColumn2 is in DateTime datatype in the database, but user input string, therefore Iam converting DateTimeColumn2 to string and try to filter to users value. Any idea, what I am doing wrong ?
What happens here is that the part...
|| m.DateTimeColumn2.ToString("dd.MM.yyyy").StartsWith(model.search.value)
...can't be translated into SQL (ToString("dd.MM.yyyy") isn't supported`), so EF-core auto-switches to client-side evaluation.
However, now the whole Where clause is evaluated client-side, including the first part,
m.Column1.Contains(model.search.value)
Now this first part has become susceptible to null reference exceptions. There are entities that have a null for Column1.
When you remove the DateTimeColumn2 predicate the whole statement can be translated into SQL and evaluated by the database.
It is likely that your DateTimeColumn2 can have NULL values which is very normal for DateTime columns. Also you shouldn't convert it to a string but the search value to a datetime. Is the user searching like "01" to mean any 1st date of any month and\year?
query = query.Where(m => m.Column1.Contains(model.search.value)
|| !m.DateTimeColumn2.HasValue
|| m.DateTimeColumn2.ToString("dd.MM.yyyy").StartsWith(model.search.value));
return query.ToList()
If you think that the exception is thrown because of any of the DateTimeColumn2 values might be null, check for non-nullness:
query = query.Where(m => ...
|| (m.DateTimeColumn2 != null &&
m.DateTimeColumn2.ToString("dd.MM.yyyy").StartsWith(model.search.value)));
I am getting this given exception
Exception in thread "Thread-2" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.freeGo.model.Pump at com.freeGo.util.HealthTask.run(HealthTask.java:33) at java.lang.Thread.run(Thread.java:745)
My code is :
#Query("SELECT p.id, p.timestamp FROM Pump p WHERE p.isActive = :isActive")
public List<Pump> findByIsActive(#Param("isActive") int isActive);
if i don't use Query annotation as
public List<Pump> findByIsActive(#Param("isActive") int isActive);
then it's run successfully, but it return's all table data but i want only 2 column.
My project in spring-3 and jpa.
There is nothing wrong with the query, if that's what you want. You will be getting a object[] (object array) from the query instead of a List<Pump>. So, make the return type List<Object[]> and get id in column 0 and time in column 1.
If you want to make it a little better code, you should probably get the query to return a custom DTO.
#Query("SELECT new MyDto(p.id, p.timestamp) FROM Pump p WHERE p.isActive = :isActive")
and return a List<MyDto>
Reference: Spring JPA selecting specific columns
I am getting this runtime exception
Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to
type 'System.String'.
How can I fix my code to get my result without exceptions?
ProductsDataContext db = new ProductsDataContext();
var matchedproduct = db.GetTable<product>().SingleOrDefault(p =>p.ProductID==productID);
if (matchedproduct != null)
product.ProductName = txtpname.Text;
db.SubmitChanges();
If you aren't getting a compile time error its because your dbml doesn't accurately depict the column in your database. Your object thinks its a string but its clearly a decimal in the database. You should update it in the dbml editor. Then when you set product name you will have to parse out the decimal value from the Text.