How do I make my entity object managed so I can remove it? - jpa

This doesn't work -- I always get the IllegalArgumentException thrown with the advice to "try merging the detached and try the remove again."
#PersistenceContext private EntityManager em;
#Resource private UserTransaction utx;
public void delete(EN entity) {
if (entity == null) return; // null-safe
EN target = entity;
try {
if (!em.contains(entity)) {
System.out.println("delete() entity not managed: " + entity);
utx.begin();
target = em.merge(entity);
utx.commit();
System.out.print("delete() this entity should now be managed: " + em.contains(target));
}
utx.begin();
em.remove(target);
utx.commit();
} catch (RollbackException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
} catch (HeuristicMixedException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
} catch (HeuristicRollbackException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
} catch (IllegalStateException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
} catch (NotSupportedException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
} catch (SystemException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
}
The output log shows the following:
INFO: delete() entity not managed: com.database.SomeTable[ id=2 ]
INFO: delete() this entity should now be managed: false
In other words the merge() does not return a managed entity. Can anyone spot what I did wrong?
Second question: is there a way to do this in one transaction rather than two?
This is with EclipseLink inside GlassFish 3.1.

This snippet of code creates two transactions:
if (!em.contains(entity)) {
System.out.println("delete() entity not managed: " + entity);
utx.begin();
target = em.merge(entity);
utx.commit();
System.out.print("delete() this entity should now be managed: " + em.contains(target));
}
utx.begin();
em.remove(target);
utx.commit();
While it is true that the entity is merged into the persistence context, it is true only of the first transaction, and not for both. In the second transaction, the persistence context associated with the transaction will again find a detached object passed as an argument, in the following line:
em.remove(target);
since, the previous utx.commit() would have detached the reference to target.
To fix this, you must merge the entity into the persistence context, and delete the entity in the same transaction, before the reference is detached:
if (!em.contains(entity)) {
System.out.println("delete() entity not managed: " + entity);
utx.begin();
target = em.merge(entity);
em.remove(target);
utx.commit();
System.out.print("delete() this entity should now be deleted: " + (!em.contains(target)) );
}

After commiting your persistence context should be gone. So your println test after the commit will fail, because the "target" object is not managed anymore.
Do all your stuff within one transaction and it should work. You just have to start your transaction after your "try" begins.

Related

Retrieve a reference to JPA transaction

I'm trying to commit JPA manually
var tx = em.getTransaction();
for(var key : aggrFornitore.keySet()){
tx.begin();
try {
var righe = aggrFornitore.get(key);
jpaOperatingOnRepositories(key, righe, mSchede, mSettings);
tx.commit();
} catch (Exception ex){
ex.printStackTrace();
tx.rollback();
}
}
em is:
#PersistenceContext
EntityManager em;
JPA thorws an exception
Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead
Is it possible to control JPA transaction manager manually?
Thanks to xerx593 comment I changed my code into
for(var key : aggrFornitore.keySet()){
var txDef = new DefaultTransactionDefinition();
txDef.setIsolationLevel(DefaultTransactionDefinition.ISOLATION_READ_COMMITTED);
TransactionStatus transactionStatus = transactionManager.getTransaction(txDef);
try {
var righe = aggrFornitore.get(key);
creaSingoloOrdine(key, righe, mSchede, mSettings);
transactionManager.commit(transactionStatus);
} catch (Exception ex){
ex.printStackTrace();
transactionManager.rollback(transactionStatus);
}
}

#Transactional with handling error and db-inserts in catch block (Spring Boot)

I would like to rollback a transaction for the data in case of errors and at the same time write the error to db.
I can't manage to do with Transactional Annotations.
Following code produces a runtime-error (1/0) and still writes the data into the db. And also writes the data into the error table.
I tried several variations and followed similar questions in StackOverflow but I didn't succeed to do.
Anyone has a hint, how to do?
#Service
public class MyService{
#Transactional(rollbackFor = Exception.class)
public void updateData() {
try{
processAndPersist(); // <- db operation with inserts
int i = 1/0; // <- Runtime error
}catch (Exception e){
persistError()
trackReportError(filename, e.getMessage());
}
}
#Transactional(propagation = Propagation.REQUIRES_NEW)
public void persistError(String message) {
persistError2Db(message); // <- db operation with insert
}
You need the way to throw an exception in updateData() method to rollback a transaction. And you need to not rollback persistError() transaction at the same time.
#Transactional(rollbackFor = Exception.class)
public void updateData() {
try{
processAndPersist(); // <- db operation with inserts
int i = 1/0; // <- Runtime error
}catch (Exception e){
persistError()
trackReportError(filename, e.getMessage());
throw ex; // if throw error here, will not work
}
}
Just throwing an error will not help because persistError() will have the same transaction as updateData() has. Because persistError() is called using this reference, not a reference to a proxy.
Options to solve
Using self reference.
Using self injection Spring self injection for transactions
Move the call of persistError() outside updateData() (and transaction). Remove #Transactional from persistError() (it will not work) and use transaction of Repository in persistError2Db().
Move persistError() to a separate serface. It will be called using a proxy in this case.
Don't use declarative transactions (with #Transactional annotation). Use Programmatic transaction management to set transaction boundaries manually https://docs.spring.io/spring-framework/docs/3.0.0.M3/reference/html/ch11s06.html
Also keep in mind that persistError() can produce error too (and with high probability will do it).
Using self reference
You can use self reference to MyService to have a transaction, because you will be able to call not a method of MyServiceImpl, but a method of Spring proxy.
#Service
public class MyServiceImpl implements MyService {
public void doWork(MyService self) {
DataEntity data = loadData();
try {
self.updateData(data);
} catch (Exception ex) {
log.error("Error for dataId={}", data.getId(), ex);
self.persistError("Error");
trackReportError(filename, ex);
}
}
#Transactional
public void updateData(DataEntity data) {
persist(data); // <- db operation with inserts
}
#Transactional
public void persistError(String message) {
try {
persistError2Db(message); // <- db operation with insert
} catch (Exception ex) {
log.error("Error for message={}", message, ex);
}
}
}
public interface MyService {
void doWork(MyService self);
void updateData(DataEntity data);
void persistError(String message);
}
To use
MyService service = ...;
service.doWork(service);

EntityManager in Transactional CDI bean

I have an EntityManager associated with my persistence unit (myPU).
I have the following code which represents a generic DataAccessObject which I want to use in order to execute tasks in a new transaction (requires-new).
This DataAccessObject gets injected into an EJB and its unique method gets invoked in a while loop.
Another EntityManager instance referencing the same persistence unit exists in the EJB.
I'm expecting that at every method invocation of my DataAccessObject instance, a new transaction gets created and committed (or rollbacked) according to the following code.
The problem is that i get a transaction required exception. What am i missing?
#Dependent
#ManagedBean
public class DataAccessObject {
private static final Logger logger = Logger.getLogger(DataAccessObject.class);
#PersistenceContext(unitName = "scheduler")
private EntityManager entityManager;
#Transactional(value = TxType.REQUIRES_NEW, rollbackOn = Exception.class)
public void executeInNewTransaction(TransactionalTask transactionalTask) throws TransactionalException {
Throwable exception = null;
try {
logger.debug(" A new transaction has been created for transactional task: \"", transactionalTask, "\".");
transactionalTask.onExecute(entityManager);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
exception = e;
} catch (ConstraintViolationException e) {
Set<ConstraintViolation<?>> constraintViolation = e.getConstraintViolations();
logger.error("Exception during bean validation:");
if (constraintViolation != null) {
for (ConstraintViolation<?> violation : constraintViolation) {
logger.error(String.format("%s=\"%s\" error: %s", violation.getPropertyPath(), violation.getInvalidValue(), violation.getMessage()));
}
}
exception = e;
} catch (Throwable e) {
exception = e;
} finally {
if (exception != null || transactionalTask.mustRollBack()) {
throw new TransactionRolledBackException("Transaction is being rolled back for transactional task: \"" + transactionalTask + "\".", exception);
} else {
logger.debug(" Transaction has been committed successfully for transactional task: \"", transactionalTask, "\".");
}
}
}
}

CRM 2011 Plugin won't update and get emails from ActivityParty

I'm trying to get all emails (to, from, cc) from an email in a list and go through the list and check the contacts, if the Contact exists in CRM then a field on the email entity will be marked as true. When I check the to, from, and cc fields of the email it returns 0 parties, but there is no error there. Also at the end, when I'm calling service.Update(entity), it returns an error. An unexpected error occurred.
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider
.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider
.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory
.CreateOrganizationService(context.UserId);
try
{
Email entity;
if (context.MessageName == "Create")
{
if (context.PostEntityImages.Contains("PostImage")
&& context.PostEntityImages["PostImage"] is Entity)
entity = (Email)context.PostEntityImages["PostImage"].ToEntity<Email>();
else
throw new Exception("No PostEntityImages...");
}
else
throw new Exception("EmailPortalVisibilityPlugin Plugin invalid");
if(entity.LogicalName != "email")
throw new Exception("EmailPortalVisibilityPlugin invalid");
bool contactExists = false;
List<string> emails = new List<string>();
emails.AddRange(ParseAddressUsed(entity.To, trace));
emails.AddRange(ParseAddressUsed(entity.From, trace));
emails.AddRange(ParseAddressUsed(entity.Cc, trace));
foreach (String em in emails)
{
contactExists = LookupContact(em, service, trace);
if (contactExists)
break;
}
UpdateToggleState(entity, contactExists, service, trace);
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException("Execute '" + ex.Message + "'");
}
}
public List<string> ParseAddressUsed(
IEnumerable<ActivityParty> entity, ITracingService trace)
{
try
{
List<string> addressStrings = new List<string>();
foreach (ActivityParty party in entity)
addressStrings.Add(party.PartyId.Id.ToString());
return addressStrings;
}
catch (FaultException<OrganizationServiceFault> exceptionServiceCall)
{
throw new Exception("ParseAddressUsed FaultException");
}
catch (Exception ex)
{
throw new Exception("ParseAddressUsed Exception");
}
}
public bool LookupContact(
String emailAddress, IOrganizationService service, ITracingService trace)
{
try
{
QueryByAttribute queryByAttribute = new QueryByAttribute("contact");
queryByAttribute.ColumnSet = new ColumnSet("contactId");
queryByAttribute.Attributes.Add("emailaddress1");
queryByAttribute.Values.Add(emailAddress);
EntityCollection retrieved = service.RetrieveMultiple(queryByAttribute);
return (retrieved.Entities.Count > 0);
}
catch (FaultException<OrganizationServiceFault> exceptionServiceCall)
{
throw new Exception("LookupContact Exception");
}
catch (Exception ex)
{
throw new Exception("LookupContact Exception");
}
}
public void UpdateToggleState(
Email entity, bool toggleState, IOrganizationService service, ITracingService trace)
{
try
{
Entity email = new Entity("email");
email.Id = entity.Id;
email.Attributes.Add("new_clientfacing", toggleState);
service.Update(email);
}
catch (FaultException<OrganizationServiceFault> exceptionServiceCall)
{
throw new Exception("UpdateToggleState Exception");
}
catch (Exception ex)
{
throw new Exception("UpdateToggleState Exception");
}
}
Try to set the first argument type of function ParseAddressUsed to EntityCollection instead of IEnumerable<ActivityParty>, and do the necessary changes.
And for the final update in function UpdateToggleState, there is no need to create a new email Entity (Entity email = new Entity("email");), when you already have the entity variable. You could just set the new_clientfacing attribute and update the entity, which is already retrieved.
In your method ParseAddressUsed you are adding the PartyId GUID to the string list and you use it in LookupContact in the emailaddress1 filter as a parameters, that is probably the reason why you are not retrieving any records.
Please try to change addressStrings.Add(party.PartyId.Id.ToString()) to addressStrings.Add(party.AddressUsed) instead and see if that works.
Cheers, dimamura

Update only scalar properties in Code First, EF 5

God day!
I have a tree of entities and at specific point of time i need to update only scalar properties of one entity. Classic update rise entire graph lookup, but relations not need to update.
The trouble in Category entity what one category have another categories in children. My method generate exceptions when saving changes about duplicate key. I think EF try to add children to database.
Static method of my data context listed below:
public static void Update<T>(T item) where T : KeyedObject
{
if (item == null)
throw new ArgumentNullException("Item to update is null");
item.ValidateIsNotNew();
using (DataContext db = new DataContext())
{
T original = GetOriginalWithException<T>(db, item);
DbEntityEntry entry = db.Entry(original);
entry.CurrentValues.SetValues(item);
entry.State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (Exception ex)
{
throw new DatabaseException(
"Cant update list item. See inner exception for details.",
ex);
}
}
}
I tries another method: attaching object. This method does not throw exception, but it rise entire graph update and take many resources. Code listed below:
public static void Update<T>(T item) where T : KeyedObject
{
if (item == null)
throw new ArgumentNullException("Item to update is null");
item.ValidateIsNotNew();
using (DataContext db = new DataContext())
{
db.Set<T>().Attach(item);
db.Entry(item).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (Exception ex)
{
throw new DatabaseException(
"Cant update list item. See inner exception for details.",
ex);
}
}
}