Which do you prefer? When you use JPA in a Web MVC...
# codingconventions
e
Which do you prefer? When you use JPA in a Web MVC Spring framework application, what do you do when defining the
@ID
of an entity class? And
@Id
property is generated by database autoincrement. In this question, pk is not-null in the database, and there is discussion within the team about what value should be set as default before Entity persisted, so we are also asking the community's opinion. In this case, I dont discuss about using constructor each time when create instance.
Copy code
1. nullable - var
@Entity
class Entity(
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  var id: Long? = null
)

2. nullable - val
@Entity
class Entity(
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  val id: Long? = null
)

3. non-null - var
@Entity
class Entity(
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  var id: Long = 0L
)

4. non-null - val
@Entity
class Entity(
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  val id: Long = 0L
)
Please share your voice and opinion free!
1️⃣ 1
2️⃣ 2
3️⃣ 1
4️⃣ 10
l
I'll always go for non nullable val here. Never having to make checks for nullability and not having a chance of having it mutated by other parts of the application