Handling @Transactional rollbackFor with @Where clause

This is my Entity:

@Entity
@Table(name = "example")
@Where(clause = "count >= 0")
public class Example {
  @Column(name="count")
  private Integer count;

  //some code
}

This is my DML operation:

@Transactional(rollbackFor = Exception.class)
public void Example {
    update();
}

void update(){
    // findOne(Id) - if null throw Exception
    // --count & save()                        //line 8
    // findOne(Id) if null throw Exception     //line 9
    // --count & save()


    // if something fails 
    throw new Exception("Exception thown.");
}

Suppose initially value of count is 0 for Id in db. line 9 will also return a records since line 8 has not been commited yet. There is no exception thrown. It will not rollback and update the db, which should not happen. Can anyone help, If any hibernate/JPA solution exists for this.

Reading file available in resources folder with SpringBoot

I am going to read a file available in resources folder in my Springboot application. I have used ResourceLoader to do it. But I get a FileNotFoundException when I try to execute my application.

Error creating bean with name 'demoController': Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [schema.graphql] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/home/dilan/Projects/demo/target/demo.jar!/BOOT-INF/classes!/schema.graphql
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137) ~[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409) ~[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620) ~[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]

Below is my Code

 @Autowired
private ResourceLoader resourceLoader;

final Resource fileResource = resourceLoader.getResource("classpath:schema.graphql");
File schemaFile = fileResource.getFile();
TypeDefinitionRegistry definitionRegistry = new SchemaParser().parse(schemaFile);
RuntimeWiring wiring = buildRuntimeWiring();

Can anybody please help me to sort this out

Get file from path by ADB shell

I’m writing automated tests and I’m using ADB intents to run commands.

For now I’m connecting with device. Running DFU Mode on connected device.

And There is a problem.

I wan’t to specify path to file on phone by ADB command and using
Nordic Library I want to update this file on the device.

ERROR:
DfuBaseService: A security exception occurred while opening file java.lang.SecurityException: Permission Denial: opening provider com.android.externalstorage.ExternalStorageProvider requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS

I tried with granting permission to READ/WRITE External storage but with n o luck.

Thx for any advice and proposition.

Why may setAdapter() on my ListView produce a NullPointerException?

I’m confused about this situation. The code below works fine, but Android Studio alerts me that setAdapter() may produce ‘java.lang.NullPointerException’:

public class PlayerManagement extends AppCompatActivity {
    private Cursor c;
    private SQLiteDatabase db;
    private String selectedPlayerId, selectedPlayerName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player_management);
        ListView listView = (ListView) findViewById(android.R.id.list);

        try {
            MTGPS_DBHelper dbHelper = new MTGPS_DBHelper(this);
            db = dbHelper.getWritableDatabase();
            new getPlayersCursorAsync().execute();

            final SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(PlayerManagement.this, android.R.layout.simple_list_item_1, c,
                                       new String[]{MTGPS_DB_Contract.Player.COLUMN_NAME_PLAYER_NAME},
                                       new int[]{android.R.id.text1}, 0);
        listView.setAdapter(mAdapter);

...

And this is the XML file activity_player_management:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="match_parent">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no_players_found"
        android:textSize="25sp" />

</LinearLayout>

As I say, everything works fine, but I don’t get why Android Studio alerts me. If I declare the ListView as a class field, then the “problem” disappears. Only when I declare the Listview inside the onCreate method is when I get the alert.

Android date format

I don’t understand how work java SimpleDateFormat. I want to show date and time by Locale.

For example

Locale.setDefault(Locale.FRANCE);
DateFormat correctDateFormatter =SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT, Locale.getDefault());
correctDateFormatter.format(date);

So I wait to output in 24-h format like

23:00

but I get it like

11 PM

How I can show time and date depends on the Local and do no not use time pattern(“hh:mm” and so on)?

How to add plugin to init.gradle?

I need to add mirror repositories to Gradle. For that I use gradle-maven-plugin

Is it way to add this plugin to init.gradle?

I tried to add like this:

buildscript{
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "gradle.plugin.com.palantir.gradle.docker:gradle-docker:0.13.0"
    }
}

plugins {
  id "net.linguica.maven-settings" version "0.5"
}

mavenSettings {
    userSettingsFileName = './settings.xml'
}

But I have an error:

FAILURE: Build failed with an exception.

* Where:
Initialization script '/root/projects/gradle/HRI_Zagrebelskii/init.gradle' line: 12

* What went wrong:
A problem occurred evaluating initialization script.
> Plugin with id 'net.linguica.maven-settings' not found.

Where I’m wrong?

Creating map from list, where key is in part of inner and outer object

Is it possible (in simple way) to change it into java8 Stream? (Please do not comment/answer if you want to tell me that two for are better and not all loops should be changed to streams, it’s not a point)

final Map<String, String> map = new HashMap<>();

for(final Person person: list) {
    for(final Internal internal: person.getInternals()) {
        final String key = person.getName() + internal.getKey();
        map.put(key, internal.getValue());
    }
}

The main problem is that I can’t use flatMap because I will lose previous information. Each created key is unique.

Math calculation for angle between two points Java

I am trying to make a calculation for an angle between my mouse and the ‘player’ Im using lwjgl and opengl. This is what is use to rotate the image:

GL11.glTranslatef(p.getPosition().x+playerTexture.getTextureWidth()/2, p.getPosition().y+playerTexture.getTextureHeight()/2, 0);
GL11.glRotated(0, 0.0f, 0.0f, 1.0f);
GL11.glTranslatef(-p.getPosition().x+-playerTexture.getTextureWidth()/2, -p.getPosition().y+-playerTexture.getTextureHeight()/2, 0);`

I have tried two things but everytime it just keeps spinning the image non stop. My attempts:

public float getAngle(Player p) {
    float angle = (float) (Math.atan2(Mouse.getY() - 
    p.getPosition().y+playerTexture.getTextureHeight()/2 , Mouse.getX() - 
    p.getPosition().x+playerTexture.getTextureWidth()/2));
    return angle;
}

public void calcAngle(Player p){
    double y = Mouse.getY() - p.getPosition().y;
    double x = Mouse.getX() - p.getPosition().x;
    double dir = Math.atan2(y, x);
}

I hope someone knows a fix for this. Thanks.