Result of dao.createOrUpdate() in ORMLite - ormlite

I wonder if it is possible to find out if ORMLite's dao.createOrUpdate() method has actually created or updated a table row. There is a result Object (CreateOrUpdateStatus), which contains these informations, but all indicating fields (created, updated and numLinesChanged) are package-visible only. Is that a bug or does anyone have an idea why is that so?
Thanks.

Oh what an epic fail. This is definitely a bug which was fixed in version 4.25 on 8/22/2011. Sorry about this #steffen.
The permissions on that class have been corrected:
public class CreateOrUpdateStatus {
...
public boolean isCreated() {
return created;
}
public boolean isUpdated() {
return updated;
}
public int getNumLinesChanged() {
return numLinesChanged;
}
}

Related

How to rewrite Uni<List<Fruit>> to Multi without a list? Reactive Programming

As i am working in a Project where i want to rewrite the Uni to Multi for a method "findall" to get all the mongodb Document from a collection. I tried to rewrite but not able to find a solution
original:
public Uni<List<Book>> findAll(List<String> authors)
{
return getCollection().
find(Filters.all("authors",authors)).map(Book::from).collectItems().asList();
}
What i tried (But not working)
public Multi<Book> findAll(List<String> authors)
{
return getCollection().find(Filters.all("authors",authors)).transform().
byFilteringItemsWith(Objects::nonNull).onCompletion().ifEmpty().
failWith(new NoSuchElementException("couldn't find the Authors")).onItem().transform(Book::from);
}
I suppose you are using the ReactiveMongoClient provided by Quarkus.
In this case, your method should be:
ReactiveMongoClient client;
public ReactiveMongoCollection<Book> getCollection() {
return client.getDatabase("db").getCollection("books", Book.class);
}
public Multi<Book> findAll(List<String> authors) {
return getCollection()
.find(Filters.all("authors",authors))
.onItem().transform(Book::from)
.onCompletion().ifEmpty()
.failWith(new NoSuchElementException("..."));
}
You don't need to do thebyFilteringItemsWith, as a Multi cannot contain null items.

delete list using JPA

I have list to be deleted.
My code to delete my list is:
for (MyDataModel dataMo: listData) {
testEJB.delete(dataMo.getPkId(), MyDataModel.class);
}
public void delete(Object id, Class<T> classe) {
T entityToBeRemoved = em.getReference(classe, id);
em.remove(entityToBeRemoved);
}
Since my list size may be more than 500, data deletion by this method is much time consuming.I want alternative so that deletion is quicker.I need help.
Ok i have got solution on my own for this i used native query.Here i do not have to generate the list too.My code is:
public int deleteUsingNativeQuery(String query){
Query qry = em.createNativeQuery(query );
return qry.executeUpdate();
}
Here i pass the native query "delete from 'table name' where 'condition'" in function deleteUsingNativeQuery and deletion was also quick.

Making EntityFramework set one property first, before the others

When using code-first EntityFramework, I need one property to be set before the others - how do I specify the order that it calls the property sets, when it is creating the objects from the database?
E.g.
public class Person
{
public string Name { get; set; }
public string Something
{
get { return something; }
set
{
something = value + " for " + Name;
}
}
private string something;
}
In the code above, I need the Name property to already have been set by the time it sets the Something property.
This isn't the actual example - I know there are other ways to achieve that literally, but I'm not after those, just how I can tell EF to set Name before Something.
I am trying to understand the context of your question. I am going to make the assumptions that:
The value passed to the setter is not another calculated property
the value passed to the setter is intended to be stored in the database
If name is updated you would want Something to be updated to reflect the new name?
I think your mistake here is trying to add a derived portion to the value you are looking to store. Calculate the pretty name in another property, or on a get:
UPDATE had an example overriding the get on the Something Property, but removed as I feel it is bad practice.
public class Person
{
public string Name { get; set; }
public string Something { get; set; }
public string getFancySomething {
{ return Something + " for " + Name; }
}
}
Finally - (and here is where I could use some help as I have not run into a situation where I needed to do something like this) my guess is that you do not need to be storing the partially calculated property Something in the way you were attempting, but if you do need to, I think additional detail might help someone provide you with a better answer.
UPDATE 2
As described in my comments - not sure this would work, and it feels very wrong, but you could try something like:
modelBuilder.Entity<Person>().Ignore(x => x.Something);
and then in the setter:
public class Person
{
public string Name {
get { return Name; }
set {
Name = value;
Something = lookup();
}
}
}
Again this will depend on you needs, and would not satisfy any need to pass this value in, and I am not sure this is a great idea.

Reading integers from AppSettings over and over

Some I do quite a lot of is read integers from AppSettings. What's the best way to do this?
Rather than do this every time:
int page_size;
if (int.TryParse( ConfigurationManager.AppSettings["PAGE_SIZE"], out page_size){
}
I'm thinking a method in my Helpers class like this:
int GetSettingInt(string key) {
int i;
return int.TryParse(ConfigurationManager.AppSettings[key], out i) ? i : -1;
}
but this is just to save some keystrokes.
Ideally, I'd love to put them all into some kind of structure that I could use intellisense with so I don't end up with run-time errors, but I don't know how I'd approach this... or if this is even possible.
What's a best practices way of getting and reading integers from the AppSettings section of the Web.Config?
ONE MORE THING...
wouldn't it be a good idea to set this as readonly?
readonly int pageSize = Helpers.GetSettingInt("PAGE_SIZE") doesn't seem to work.
I've found an answer to my problem. It involves extra work at first, but in the end, it will reduce errors.
It is found at Scott Allen's blog OdeToCode and here's my implementation:
Create a static class called Config
public static class Config {
public static int PageSize {
get { return int.Parse(ConfigurationManager.AppSettings["PAGE_SIZE"]); }
}
public static int HighlightedProductId {
get {
return int.Parse(ConfigurationManager.AppSettings["HIGHLIGHT_PID"]);
}
}
}
Advantage of doing this are three-fold:
Intellisense
One breakpoint (DRY)
Since I only am writing the Config String ONCE, I do a regular int.Parse.
If someone changes the AppSetting Key, it will break, but I can handle that, as those values aren't changed and the performance is better than a TryParse and it can be fixed in one location.
The solution is so simple... I don't know why I didn't think of it before. Call the values like so:
Config.PageSize
Config.HighlightedProductId
Yay!
I know that this question was asked many years ago, but maybe this answer could be useful for someone. Currently, if you're already receiving an IConfiguration reference in your class constructor, the best way to do it is using GetValue<int>("appsettings-key-goes-here"):
public class MyClass
{
private readonly IConfiguration _configuration;
public MyClass(IConfiguration configuration)
{
_configuration = configuration;
}
public void MyMethod()
{
int value = _configuration.GetValue<int>("appsettings-key-goes-here");
}
}
Take a look at T4Config. I will generate an interface and concrete implementation of your appsettings and connectionstringsections of you web/app config using Lazyloading of the values in the proper data types. It uses a simple T4 template to auto generate things for you.
To avoid creating a bicycle class you could use the following:
System.Configuration.Abstractions.AppSettings.AppSetting<int>("intKey");
https://github.com/davidwhitney/System.Configuration.Abstractions

Extending IEnumerable to Return BindingList

In a previous question on Stack Overflow, I had run into an issue with returning an EF query to the DataGridView. Of course I'd run into an issue. However, I added an extension method that still has me baffled since it isn't working. It seems like it should, but for some reason it's not.
public static class BindingListEntityExtension
{
public static BindingList<T> ToBindingList<T>(this IEnumerable<T> entities)
{
BindingList<T> rtn = new BindingList<T>();
foreach (T obj in entities)
{
rtn.Add(obj);
}
return rtn;
}
}
Any ideas what's going on? My implementation is like so:
MyEntities context = new MyEntities();
tempDataGridView.DataSource = context.Employees.ToBindingList();
Got it. As Ecyrb had suggested in a previous post, the BindingList does not sort. I did use the suggested site/ to get my list to sort. Thanks guys! My extension does work now.