Styles

Showing posts with label mySql. Show all posts
Showing posts with label mySql. Show all posts

Tuesday, June 18, 2024

Hibernate JPA mapping using @Query done right

Hibernate is a very powerful ORM. Its been around for a while, but sometimes the magic under the hood can leave you for hours scratching your head trying to figure out why a perfectly normal sql query isn't mapping correctly to your POJO.

Consider the following query.

@Query(nativeQuery = false, value = """
    select
        t.abn,
        t.accountNumber,
        count(1) transactionCount,
        sum(t.balance) totalAmount
    from AccountTransaction t
    where t.reportDate = :reportDate
    group by t.abn, t.accountNumber
    """
)
fun findTotalAmountByDate(
    @Param("reportDate") reportDate: LocalDate
): List<AccountTransactionEntity>
The Entity that should be mapped to the results is as follows.
@Entity
data class AccountTransactionEntity(
    val abn: String,
    val accountNumber: String,
    val transactionCount: Int,
    val totalAmount: BigDecimal
)
At first glance it seems to be perfectly fine. However looking at the logs, the following error occurs.

org.springframework.core.convert.ConversionFailedException:
  Failed to convert from type [java.lang.Object[]]
  to type [AccountTransactionEntity] for value '{67220345566, 200300809, 8, -2459.25}';
nested exception is org.springframework.core.convert.ConverterNotFoundException:
  No converter found capable of converting from type [String]
  to type [AccountTransactionEntity]

The reason for this is that it doesn't understand how to bind the query result set implicitly with a strongly typed object using inference. You'll need to help hibernate a little by making an explicit instantiation within the non-native query as follows.
@Query(nativeQuery = false, value = """
    select new AccountTransactionEntity(
        t.abn,
        t.accountNumber,
        count(1) transactionCount,
        sum(t.balance) totalAmount
    ) from AccountTransaction t
    where t.reportDate = :reportDate
    group by t.abn, t.accountNumber
    """
)
fun findTotalAmountByDate(
    @Param("reportDate") reportDate: LocalDate
): List<AccountTransactionEntity>
That will compile fine and won't need any further transformation downstream.

Thursday, March 4, 2010

Apache Configuration

XAMPP Server Configuration

When installing xampp, generally you wouldn't want to keep all your PHP applications nested within the installed folders incase you choose to uninstall it for a newer version. The following file will need to be configured to place php applications in your own project folder (e.g. D:\Projects\Php\)

C:\Program Files\xampp\apache\conf\httpd.con

DocumentRoot "D:/Projects/Php"
<Directory "D:/Projects/Php">
     Options Indexes FollowSymLinks Includes ExecCGI
     AllowOverride All
     Order allow,deny
     Allow from all
 </Directory>

DocumentRoot for phpMyAdmin

When changing the DocumentRoot as shown above, this will change the location of the mySQL admin console. Therefore a shortcut for "phpMyAdmin" must be created on D:/Projects/Php from C:\Program Files\xampp\phpMyAdmin. This will allow for access to mySQL via http://localhost/phpMyAdmin

Default Documents

If you don't want to specify the .php extension in the URL by having a default php load within any folder on your site, you can add any specific php file names to the following file:

C:\Program Files\xampp\apache\conf\httpd.conf

DirectoryIndex index.php

XAMPP mySQL databases location

Databases can generally be copied and pasted from the following location:

C:\xampp\mysql\data

Database Corruption

Sometimes the database can get corrupt from copying files from the \data\ directory. This will need a reboot of the xampp server. Once this happens there will be an error message on the site saying that "[table_name] has crashed or is corrupt and will need a repair". If that is the case you must run the following on the mySQL database:

REPAIR TABLE [table_name]

Default "root" User for mySQL databases

The default password for root is blank, however changing this can be done from the xampp control panel by click the "Shell" button and typing the following command

mysqladmin -u root -p password NEWPASSWORD

Where NEWPASSWORD is the password you want to specify

Default "root" password for phpMyAdmin


The following lines need to be updated in the file: C:\xampp\phpMyAdmin\config.inc.php

$cfg['Servers'][$i]['user']                 = 'root';
$cfg['Servers'][$i]['password']             = 'NEWPASSWORD';