How to create member variables with JPA Model

I am a beginner with less than a month of java history. I will leave it as a memorandum for myself.


What I wanted to do

To the model class found from table I wanted to have a parameter with a field name that doesn't exist in the column. In php (I can't recommend it very much), if you declare the variable as it is, you can use it normally. I thought I could do it with java.

A bad example

There is no name column in the Hoge table When you want to get it from the outside or dynamically process it with hoge

@Entity
public class hoge {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;

If this happens

Caused by: java.sql.SQLException: Column 'name' not found.

I got scolded. Like PHP, it doesn't come in as null It seems to be useless because there is no data to see

Solution

Use the @Transient annotation to exclude it from mapping. Originally it seems to be an annotation to exclude it from persistence It seems that it will be excluded from mapping due to that effect (I'm sorry if it is different)

@Entity
public class hoge {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Transient
    private String name;

I'm no longer angry


When studying java You can see how well I was coding in PHP.

Recommended Posts

How to create member variables with JPA Model
How to create multiple pull-down menus with ActiveHash
How to embed JavaScript variables in HTML with Thymeleaf
How to make a factory with a model with polymorphic association
How to create an application
How to number (number) with html.erb
How to update with activerecord-import
How to use Java variables
How to create a method
How to set environment variables when using Payjp with Rails
How to use trained model of tensorflow2.0 with Kotlin / Java
How to scroll horizontally with ScrollView
How to get started with slim
How to enclose any character with "~"
[How to install Spring Data Jpa]
[Processing × Java] How to use variables
[Java] How to create a folder
How to use mssql-tools with alpine
How to name variables in Java
How to get along with Rails
How to start Camunda with Docker
How to create your own Controller corresponding to / error with Spring Boot
How to rename a model with foreign key constraints in Rails
[For those who create portfolios] How to use binding.pry with Docker
How to adjustTextPosition with iOS Keyboard Extension
How to share files with Docker Toolbox
How to compile Java with VsCode & Ant
How to quickly create a reverse proxy that supports HTTPS with Docker
[Java] How to compare with equals method
How to name variables 7 selections of discomfort
[Android] How to deal with dark themes
How to use BootStrap with Play Framework
How to switch thumbnail images with JavaScript
[Note] How to get started with Rspec
How to create a Maven repository for 2020
How to achieve file download with Feign
How to update related models with accepts_nested_attributes_for
How to create a server executable JAR and WAR with Spring gradle
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to implement TextInputLayout with validation function
[Swift5] How to create a splash screen
[rails] How to create a partial template
How to handle sign-in errors with devise
How to delete data with foreign key
How to use environment variables in RubyOnRails
How to test private scope with JUnit
How to monitor nginx with docker-compose with datadog
[Spring MVC] How to pass path variables
How to deal with Precompiling assets failed.
How to achieve file upload with Feign
How to run Blazor (C #) with Docker
How to build Rails 6 environment with Docker
How to store data simultaneously in a model associated with a nested form (Rails 6.0.0)
Sample to create PDF from Excel with Ruby
How to download Oracle JDK 8 rpm with curl
[Java] How to test for null with JUnit
How to mock each case with Mockito 1x
How to create a database for H2 Database anywhere
Summary of how to create JSF self-made tags
[Rails] rails new to create a database with PostgreSQL
[Rails] How to create a graph using lazy_high_charts