私は誕生日から年齢を計算しようとしています(@Formula
)。@Formulaで誕生日からの年齢を計算する
@Entity
@Table(name = "users")
public final class UserProfile {
//this column is called BIRTHDATE in database
private String birthdate;
@Formula("(select TIMESTAMPDIFF(YEAR,BIRTHDATE,CURDATE()) from users)")
private String age;
//more things...
}
しかし、私はエラーを取得する:
はMySQL documentationよると、私はこれを試してみました私はphpMyAdminの中にそのクエリを実行した場合
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'userprofil0_.YEAR,userprofil0_.BIRTHDATE,CURDATE()) from users) as formula0_0_ f' at line 1
は、それが正常に動作します。
thisに続いて、私はまたして試してみました:
@Formula("date_part('year', age(birthdate))")
private String age; //I tried with int instead of String, too
しかし、私はこのエラーを持っている:
java.sql.SQLSyntaxErrorException: FUNCTION userprofile-service-db.date_part does not exist
この1でもphpMyAdminのでは動作しませんが...
任意のアイデア?
EDIT
私だけで試してみました:
@Formula("(TIMESTAMPDIFF(YEAR,BIRTHDATE,CURDATE()))")
private String age;
が、まだ機能していません。これは、Hibernateが表示するクエリです:Hibernateは列としてYEAR
を取っているように見えます
select
userprofil0_.id as id1_1_0_,
userprofil0_.about_me as about_me2_1_0_,
userprofil0_.birthdate as birthdat3_1_0_,
userprofil0_.display_name as display_4_1_0_,
userprofil0_.email as email5_1_0_,
userprofil0_.profile_photo as profile_6_1_0_,
(TIMESTAMPDIFF(userprofil0_.YEAR,
userprofil0_.BIRTHDATE,
CURDATE())) as formula0_0_
from
users userprofil0_
where
userprofil0_.id=?
が、実際には予約語です。 userprofil0_.YEAR
をちょうどYEAR
と置き換えてphpMyAdminでそのクエリを実行した場合、正常に動作します。
こんにちは!私は自分の答えを更新しました。あなたはそれを試しましたか? – streetturtle