how to retrieve duplicate data using self join in detached criteria.. i wanna join Pool_id and Product_id colum - criteria

In this Detached Criteria i want to get duplicate Value from two Colum One Pool_id and Product_id
....
Object[] id1={1,2};
DetachedCriteria poolcriteria = DetachedCriteria.forClass(PoolProduct.class, "PoolProduct");
poolcriteria.add(Restrictions.in("PoolProduct.id",id1));
System.out.println("Before list...");
List<PoolProduct> results = poolcriteria.getExecutableCriteria(session).list();
System.out.println("Before loop...");
for (PoolProduct mj1 : results) {
System.out.println("Pool ID..."+mj1.getPoolProductId());
List<Product> poolProd = (List<Product>) mj1.getProduct();
for(Product mi:poolProd){
System.out.println("Product Id..."+mi.getProductId());
System.out.println("Product Deascription..."+mi.getProductDescription());
count++;
}

Related

Executing Raw sql with Entity Framework Core

The below I can have a condition to execute view/table, which would load into AccountDataModel class.
dbcontext.AccountDataModel.FromSql($"select * from account where id=123").FirstOrDefault();
How can I retrieve without using a class model, if I want to retrieve just 1 or 2 columns
example: select name from account where id=123
Do I always need a class model?
ADO.NET works in EFCore =)
using Microsoft.EntityFrameworkCore;
using System.Data.Common;
using System.Data.SqlClient;
using System;
public void ExampleMethod(DbContext context)
{
SomeObjectResult result = null;
DbCommand cmd = context.Database.GetDbConnection().CreateCommand();
cmd.CommandText = "Select C.ID, C.CarModelID as ModelID, C.VIN, C.RegNumber,cast(C.CountValue as int) as Course,A.BrandID from A inner join C on A.ID = C.KeyID Where A.ID = #appID";
cmd.Parameters.Add(new SqlParameter("#appID", appointmentID));
if (cmd.Connection.State != ConnectionState.Open)
{
cmd.Connection.Open();
}
using (var reader = await cmd.ExecuteReaderAsync())
{
if (reader.Read())
{
result = new SomeObjectResult()
{
BrandID = (int)reader["BrandID"],
Course = (int)reader["Course"],
ID = (int)reader["ID"],
ModelID = (int?)reader["ModelID"],
RegNumber = (string)reader["RegNumber"],
VIN = (string)reader["VIN"]
};
}
}
}
Yes. like this :
var account = dbcontext.AccountDataModel.FromSql(#"select a.id, a.name from account a where a.id=123");
source : FromSql for non entity type
This queries the database
var name = dbcontext.GetDBConnection().Query("select name from account where id=123").FirstOrDefault();

Trigger on a certain Account Record Type

So i have written a trigger that works perfectly fine and does exactly what i want it to do, but the thing is that it does the job on all the account, and i want it to work only on one record type of the accounts.
Can someone tell me what to add to my trigger so i can make it work only on one record type?
The following is my handler class:
public class AP03_OpportunityLineItem {
public static void preventmultipleOpportunityLineItems(List<OpportunityLineItem> listOppLineItems){
Set<Id>opportunityIds = new Set<Id>();
// get all parent IDs
for(OpportunityLineItem oli : listOppLineItems)
{
//Condition to pick certain records
opportunityIds.add(oli.OpportunityId);
}
// query for related Opportunity Line Items
Map<Id, Opportunity> mapOpportunities = new Map<Id, Opportunity>([SELECT ID,
(SELECT ID
FROM OpportunityLineItems)
FROM Opportunity
WHERE ID IN :opportunityIds]);
// opp counter of new records
Map<Id, Integer> mapOppCounter = new Map<Id, Integer>();
for(OpportunityLineItem oli : listOppLineItems)
{
if(mapOppCounter.containsKey(oli.OpportunityId))
{
mapOppCounter.put(oli.OpportunityId, mapOppCounter.get(oli.OpportunityId)+1);
}
else
{
mapOppCounter.put(oli.OpportunityId, 1);
}
}
//loop to add error if condition violated
for(OpportunityLineItem olitems : listOppLineItems)
{
if(mapOpportunities.get(olitems.OpportunityId).OpportunityLineItems.size()+mapOppCounter.get(olitems.OpportunityId)>1 || olitems.Quantity > 1)
{
olitems.addError('Ce client peut seulement loué un seul véhicule.');
}
}
}
}
The following is my PAD class:
public class PAD
{
public static String bypassTrigger; //List of bypassed triggers
public static final User user;
static {
user = [Select BypassApex__c
from User
where Id=:UserInfo.getUserId()];
bypassTrigger = ';'+ user.BypassApex__c+ ';';
System.debug('>>>>> PAD constructor : END <<<<<'+bypassTrigger);
}
/**
* Method used for the class PAD
* #param c object of type JonctionServicePrestation__c
* #return boolean
*/
public static boolean canTrigger(string Name){
return (bypassTrigger.indexof(';' + Name + ';') == -1);
}
}
And the following is my Trigger:
trigger OpportunityLineItemBeforeInsert on OpportunityLineItem (before insert) {
if(PAD.canTrigger('AP03_OpportunityLineItem')){
AP03_OpportunityLineItem.preventmultipleOpportunityLineItems(Trigger.new);
}
}
You would need to loop through your opportunitiesproducts and build a list of opportunity Id, then query the Opportunity whose Accounts in the list with the record type you want to match, and build a set of the ids that match the specified record type then check if the set contains the accountId of the opportunity being process to know if the skip or process it.
Set<Id> recordTypeOpp = new Set<ID>();
SET<Id> opportunityIds = new Set<Id>();
Id recordTypeIdYouWant = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Record Type Name').getRecordTypeId();
for(OpportunityLineItem item : listOppLineItems){
opportunityIds.add(item.OpportunityId);
}
for(Opportunity item : [SELECT Id FROM Opportunity WHERE opportunityIds IN :opportunityIds and Account.RecordTypeId = :recordTypeIdYouWant]){
recordTypeOpp.add(item.Id);
}
for(OpportunityLineItem olitems : listOppLineItems)
{
if(recordTypeOpp.contains(olitems.OpportunityId)){
//do processing
}
else {
continue;
}
}

Dynamic JPQL query qith JOIN

Had to write a jpql query, based on the input need to add and condition and for some input had to need JOIN queries.
#Override
public List<IncidentHdr> fetchIncidents(IncidentHdrDto incidentHdrDto) {
StringBuilder query = new StringBuilder();
query.append(ReposJPQL.GET_INCIDENT_DETAILS);
Map<String, Object> parameters = new HashMap<String, Object>();
List<String> criteria = new ArrayList<String>();
if(incidentHdrDto.getIncidentId() > 0) {
criteria.add("inc.incidentId = :incidentId");
parameters.put("incidentId", incidentHdrDto.getIncidentId());
}
if(incidentHdrDto.getCatCode() > 0) {
criteria.add("inc.catCode = :catCode");
parameters.put("catCode", incidentHdrDto.getCatCode());
}
if(incidentHdrDto.getType != null) {
//here i need to generate a join query
//SELECT * FROM INCIDENT JOIN CATEGORY_MAST ON(INCIDENT.CAT_CODE = CATEGORY_MAST.CAT_CODE) WHERE CATEGORY_MAST.TYPE_CODE = 16
}
Query q = em.createQuery(query.toString());
logger.info("Get Incidents Query : "+query.toString());
for (Entry<String, Object> entry : parameters.entrySet()) {
q.setParameter(entry.getKey(), entry.getValue());
}
List<IncidentHdr> incidentHdrs = q.getResultList();
return incidentHdrs;
}
where as ReposJPQL is the base query which had a where condition.
public interface ReposJPQL {
public String GET_INCIDENT_DETAILS = "SELECT inc FROM IncidentHdr inc WHERE 1 = 1" ;
}

Do I really have to update every property individually with Entity Framework?

I have an update method in my Web API repository that looks like this (in EF Core):
public async Task<Employee> Update(Employee emp)
{
Employee employee = await
_context.Employees.SingleOrDefaultAsync(e => e.ID == emp.ID);
if (employee == null)
{
return null;
}
employee.FirstName = emp.FirstName;
employee.LastName = emp.LastName;
employee.Supervisor = emp.Supervisor;
employee.OfficeBureau = emp.OfficeBureau;
employee.Notes = emp.Notes;
await _context.SaveChangesAsync();
return employee;
}
It works well enough. But do we really have to do this?
I want to do something more like this and update all entity properties in one shot:
public async Task<Employee> Update(Employee emp)
{
Employee employee = await
_context.Employees.SingleOrDefaultAsync(e => e.ID == emp.ID);
if (employee == null)
{
return null;
}
employee = emp;
_context.Update(employee);
await _context.SaveChangesAsync();
return employee;
}
I shouldn't even need this:
employee = emp;
All I should need is this:
_context.Update(emp);
So EF should say, hey, I need to update an Employee object and I know which one it is by the ID of emp you passed me on update.
But I just can't get it to work.
Does anyone know how to do this or am I really supposed to do it like in the first option?
The answer below from Dmitry is not working.
If I put a break point here:
Employee employee = await
_context.Employees.SingleOrDefaultAsync(e => e.ID == emp.ID);
and then try and step though, execution seems to get swallowed up on this line:
_context.Entry(emp).State = EntityState.Modified;
and then this is the response returned:
{}
and the employee in the Database is unchanged.
Also tried this:
public async Task<Employee> Update(Employee emp)
{
Employee employee = await
_context.Employees.SingleOrDefaultAsync(e => e.ID == emp.ID);
if (employee == null)
{
return null;
}
EntityEntry<Employee> entity = _context.Employees.Attach(emp);
entity.State = EntityState.Modified;
_context.Employees.Update(emp);
await _context.SaveChangesAsync();
return employee;
}
But same thing.
Execution gets swallowed up here:
EntityEntry<Employee> entity = _context.Employees.Attach(emp);
Where is the execution going?
Hard to tell with async sometimes.
I got it to work once like this.
Funny, I got to work right off the bat when I put it in a try/catch.
public async Task<Employee> Update(Employee emp)
{
Employee employee = await
_context.Employees.SingleOrDefaultAsync(e => e.ID == emp.ID);
if (employee == null)
{
return null;
}
try
{
_context.Employees.Update(emp);
}
catch(Exception ex)
{
throw ex;
}
await _context.SaveChangesAsync();
return emp;
}
But it only worked once.
Now it keeps throwing this exception.
{System.InvalidOperationException: The instance of entity type 'Employee' cannot be tracked because another instance of this type with the same key
is already being tracked.
When adding new entities,
for most key types a unique temporary key value will be created if no key is set (i.e. if the key property is assigned the default value for its type).
If you are explicitly setting key values for new entities,
ensure they do not collide with existing entities or temporary values generated for other new entities.
"When attaching existing entities,
ensure that only one entity instance with a given key value is attached to the context.
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap1.Add(TKey key, InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode node)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph(EntityEntryGraphNode node, Func2 handleNode)
at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState) at Lerd.Models.Concrete.EmployeeRepository.d__4.MoveNext()}"
How do I get rid of this so dbContext.Update works everytime?
public async Task<Employee> Update(Employee emp)
{
_context.Entry(emp).State = EntityState.Modified;
await _context.SaveChangesAsync();
return employee;
}
I had to change my Repo to be Scoped rather than singleton:
From
services.AddSingleton<IEmployeeRepository, EmployeeRepository>();
to:
services.AddScoped<IEmployeeRepository, EmployeeRepository>();

how could i use JPA criteria query api for joined columns?

i am new to JPA and i have a problem with it.
suppose that we have two tables which are related
by a ManytoOne association, which means that
table A stores a primary key of table B within it.
when these two tables are mapped to JPA entities
i have a problem for search on this situation.
i have used an existing code from richfaces demo, to handle filtering and sorting by using
JPA. this code is using input parameters to create criteria query.
this is the code:
private CriteriaQuery<T> createSelectCriteriaQuery() {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(entityClass);
Root<T> root = criteriaQuery.from(entityClass);
if (arrangeableState != null) {
List<Order> orders = createOrders(criteriaBuilder, root);
if (!orders.isEmpty()) {
criteriaQuery.orderBy(orders);
}
Expression<Boolean> filterCriteria = createFilterCriteria(criteriaBuilder, root);
if (filterCriteria != null) {
criteriaQuery.where(filterCriteria);
}
}
return criteriaQuery;
}
protected Expression<Boolean> createFilterCriteriaForField(String propertyName, Object filterValue, Root<T> root, CriteriaBuilder criteriaBuilder) {
String stringFilterValue = (String) filterValue;
if (Strings.isNullOrEmpty(stringFilterValue)) {
return null;
}
stringFilterValue = stringFilterValue.toLowerCase(arrangeableState.getLocale());
Path<String> expression = root.get(propertyName);
Expression<Integer> locator = criteriaBuilder.locate(criteriaBuilder.lower(expression), stringFilterValue, 1);
return criteriaBuilder.gt(locator, 0);
}
private Expression<Boolean> createFilterCriteria(CriteriaBuilder criteriaBuilder, Root<T> root) {
Expression<Boolean> filterCriteria = null;
List<FilterField> filterFields = arrangeableState.getFilterFields();
if (filterFields != null && !filterFields.isEmpty()) {
FacesContext facesContext = FacesContext.getCurrentInstance();
for (FilterField filterField : filterFields) {
String propertyName = (String) filterField.getFilterExpression().getValue(facesContext.getELContext());
Object filterValue = filterField.getFilterValue();
Expression<Boolean> predicate = createFilterCriteriaForField(propertyName, filterValue, root, criteriaBuilder);
if (predicate == null) {
continue;
}
if (filterCriteria == null) {
filterCriteria = predicate.as(Boolean.class);
} else {
filterCriteria = criteriaBuilder.and(filterCriteria, predicate.as(Boolean.class));
}
}
}
return filterCriteria;
}
the code is okay, when i try to filter columns(not joined columns), but when i try to
query on joined column, the produced query is not correct and it throws exception.
so my question is that, how could i use JPA criteria query api, to filter rows by both
joined columns and non-joined coulmns.
thanks
I don't believe you can treat join columns like regular ones.
for example if you want to filter on id of B, you would have to create a join from A to B , then use B_.id to match values.
Shay