I'm using Postgresql 9.6 and before I was using mySQL an I get the same problem with the two databases. I also have the same problem with Hibernate 4 and 5.
When I'm trying to persist an Object with a long String inside, it never ends the method session.persist(object). In the other hand it works perfectly when the string ismySQL an I get the same problem with the two databases.
not that long, like this paragraph.
addVehicle method in VehicleDAO.java
public Vehicle addVehicle(Vehicle vehicle) {
Session session =
this.sessionFactory.getCurrentSession();
session.persist(vehicle);
return vehicle;
}
Vehicle attributes:
public class Vehicle {
#Id
#Column(name = "idvehicle")
#GeneratedValue(strategy = GenerationType.IDENTITY)
int idVehicle;
#Column(name = "name")
String name;
#Column(name = "description")
String description;
#Column(name = "photob64")
String photoB64;
Eclipse console log:
Hibernate: insert into vehicle (description, name, photob64, idvehicletype) values (?, ?, ?, ?)
Hibernate doesn't persist when inserting long texts
As per your question, it doesn't persist when inserting long texts. It's is because by default String lenght is 255. You can see in database. The VARCHAR(255) lenght is 255.
You can mentioned length by using below approach:
#Column(name=" description", length = 300)
private String description;
If you are not sure of length, then it's good idea to go with lob, blob or clob as per your need. You can read details of lob, blob and clob and select one of them. For text lob or clob will work for you.
There are two ways,
You can set columnDefinition like
#Column(name = "notes", columnDefinition = "varchar(300)")
private String notes;
If you don't know maximum length of string you can set columnDefinition as text like
#Column(name = "notes", columnDefinition = "text")
private String notes;
I figured out what was happening. The method persist never ended because the information couldn't be transferred completely to the DB which was in other device, a Raspberry Pi.
I changed the MTU of the network adapters of my PC and Raspberry Pi to 1492 with the command:
sudo ifconfig eth0 mtu 1492
More info: http://www.ubuntugeek.com/how-to-change-mtu-maximum-transmission-unit-of-network-interface-in-ubuntu-linux.html
Related
Something very bizarre have been happening. I have a very simple Entity recipe like so
#Data
#Entity
#Table(name = "recipe", schema = "public")
#AllArgsConstructor
#NoArgsConstructor
public class Recipe {
#Id
#GeneratedValue(
strategy = GenerationType.IDENTITY
)
#Column(name = "id", updatable = false, nullable = false)
private long id;
#Column(name = "name")
private String name;
#Column(name = "instructions")
private String instructions;
#Column(name = "date_added", nullable = false)
private String dateAdded;
#Column(name = "last_edited", nullable = false)
private String lastEdited;
}
and I have this post service that should post the 4 string attribute to the database
public void postRecipe(Recipe recipe){
var sql = """
INSERT INTO public.recipe ("name","instructions","date_added","last_edited")
VALUES (?, ?, ?, ?)
""";
jdbcTemplate.update(
sql,
recipe.getName(),
recipe.getInstructions(),
recipe.getDateAdded(),
recipe.getLastEdited()
);
}
However when the following jason is sent using postman, I get the null value error.
{
"name":"test",
"instructions":"don't eat",
"date_added":"03/04/2017",
"last_edited":"03/04/2017"
}
ERROR: null value in column \"date_added\" of relation \"recipe\" violates not-null constraint\n Detail: Failing row contains (3, null, don't eat, null, test)
The strangest thing is that only the "name" and "instruction" columns receive their data and not the other columns. I have tried adding another String attribute to the Entity class and it also cannot get data from the jason.
Edit 1:
I have tried adding the data directly through pgadmin and it worked fine
INSERT INTO recipe (name, instructions, date_added, last_edited)
VALUES ('test', 'test instruction', '2020/03/05', '2020/05/08');
It looks like your deserialization is broken - transforming your JSON into the Java entity, which results in some null values present. Most likely because date_added != dateAdded (field name), and Jackson cannot properly set a value.
I recommend having a look at Jackson guide: https://www.baeldung.com/jackson-annotations, #JsonProperty specifically. And overall do not mix entities and DTOs
After many trials and errors I was able to come up with a solution but still have no clue as to why this is happening. It turns out the under score in the annotation is the problem.
//won't work
#Column(name = date_added)
//works
#Column(name = dateadded)
This is pretty strange because I am fairly certain that the under score is generated by hibernate.
if anyone know why this is happening please let me know... for now I will just stay away from the under scrolls.
I get this error when I try to run start my application:
org.springframework.dao.DataIntegrityViolationException: could not execute query; SQL [SELECT * FROM testquestions ORDER by id DESC LIMIT 1]; nested exception is org.hibernate.exception.DataException: could not execute query
As seen in previous problems on StackOverflow, I tried to adjust the length of my data input in my sql file and I've set the length of my #Column to the same amount of characters. this didn't help.
this is my #Table class:
#Entity
#Getter
#Setter
#Table(name = "testquestions")
public class TestQuestion {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#Column(name = "questiontitle", length = 2000)
private String questionTitle;
#Column(name = "info", length = 4096)
private String Info;
#Column(name = "solvetime")
private int solveTime;
#Column(name = "difficultylevel")
private DifficultyLevel difficultyLevel;
#Column(name = "questionimage")
private Image questionImage;
public TestQuestion(){
}
public TestQuestion(int id, String questionTitle, String info, DifficultyLevel difficultyLevel) {
this.id = id;
this.questionTitle = questionTitle;
Info = info;
this.difficultyLevel = difficultyLevel;
}
public String getInfo() {
return Info;
}
}
This is my # Query in my QuestionRepository:
#Query(value = "SELECT * FROM testquestions ORDER by id DESC LIMIT 1", nativeQuery = true)
TestQuestion fetchLastQuestion();
This is my database.sql file, it writes to a PostgreSQL data base:
TRUNCATE TABLE users CASCADE;
TRUNCATE TABLE testquestions CASCADE;
DROP TABLE users;
DROP TABLE testquestions;
CREATE TABLE users(
id int,
username varchar(255),
password varchar(255),
role varchar(255)
);
CREATE TABLE testquestions(
id int primary key ,
questiontitle varchar(2000),
info varchar(4096),
solvetime int,
difficultylevel varchar(255),
questionimage bytea
);
INSERT INTO users(id, username, password, role)
VALUES (0, 'user', 'u', 'user'),
(1, 'user','u','user');
INSERT INTO testquestions(id,questiontitle, info, solvetime, difficultylevel, questionimage)
VALUES (0, 'Multiple Databases', 'A company wants to use Spring Boot in a web application which should use JPA as a database abstraction. The unit tests should be run on an H2 database while the production should run on a MySQL database.
Select all the things that need to be done or that will be done automatically by Spring Boot.', 3, 'Easy',
''),
(1, 'Screen Orientation', 'Which of these methods are called when the screen changes orientation from portrait to landscape in Android?',
3, 'Easy',''),
(2, 'Merge Names', 'Implement the uniqueNames method. When passed two arrays of names, it will return an array containing the names that appear in either or both arrays. The returned array should have no duplicates.
For example, calling MergeNames.uniqueNames(new String[]{''Ava'', ''Emma'', ''Olivia''}, new String[]{''Olivia'', ''Sophia'', ''Emma''}) should return an array containing Ava, Emma, Olivia, and Sophia in any order.',
10, 'Easy',''),
(3, 'Date', 'Write a function that converts user entered date formatted as M/D/YYYY to a format required by an API (YYYYMMDD). The parameter "userDate" and the return value are strings.
For example, it should convert user entered date "12/31/2014" to "20141231" suitable for the API.', 10, 'Easy', ''),
(4, 'Inspector', 'Fix the bugs in the following HTML code.', 10, 'Easy',''),
(5, 'Train Composition', 'A TrainComposition is built by attaching and detaching wagons from the left and the right sides, efficiently with respect to time used.
For example, if we start by attaching wagon 7 from the left followed by attaching wagon 13, again from the left, we get a composition of two wagons (13 and 7 from left to right). Now the first wagon that can be detached from the right is 7 and the first that can be detached from the left is 13.
Implement a TrainComposition that models this problem.', 20, 'Hard', '');
Has anyone got an idea how to fix this error?
Thanks!
Tom
Hello i'm trying to learn Quarkus with Hibernate but i've ran into an issue the schema-validation.
The error:
2021-12-29 16:05:14,915 ERROR
[io.qua.hib.orm.run.sch.SchemaManagementIntegrator] (Hibernate
post-boot validation thread for ) Failed to validate Schema:
Schema-validation: wrong column type encount ered in column [BED_INFO]
in table [ROOM]; found [bpchar (Types#CHAR)], but expecting [char
(Types#VARCHAR)] 2021-12-29 16:05:14,921 ERROR
[io.qua.hib.orm.run.sch.SchemaManagementIntegrator] (Hibernate
post-boot validation thread for ) The following SQL may
resolve the database issues, as generated by the Hibernate schema
migration tool. WARNING: You must manually verify this SQL is correct,
this is a best effort guess, do not copy/paste it without verifying
that it does what you expect.
the class Room looks like this
#Entity
#Table(name = "ROOM")
public class Room {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ROOM_ID")
private long id;
#Column(name = "NAME")
private String name;
#Column(name = "ROOM_NUMBER")
private String roomNumber;
#Column(name = "BED_INFO", columnDefinition = "char")
private String bedInfo;
public Room(String name, String roomNumber, String bedInfo) {
this.name = name;
this.roomNumber = roomNumber;
this.bedInfo = bedInfo;
}
}
and the mysql schema like this
CREATE TABLE ROOM(
ROOM_ID BIGSERIAL PRIMARY KEY,
NAME VARCHAR(16) NOT NULL,
ROOM_NUMBER CHAR(2) NOT NULL UNIQUE,
BED_INFO CHAR(2) NOT NULL
);
According to their documentation this "should" work but i'm perhaps missing something here.
[BED_INFO] in table [ROOM]; found [bpchar (Types#CHAR)], but expecting [char (Types#VARCHAR)]
Means that Hibernate has found a bpchar(char) where it would expect a varchar. It seems that columnDefinition does not handle char. If you really want it to be a char, try bpchar instead.
In case it doesn't work, try running your quarkus app in dev mode with this option in the application.properties file.
quarkus.hibernate-orm.database.generation=create
This will generate the actual DDL for your database that Hibernate is expecting.
Personally I would refrain from columnDefinition, instead i would use length and nullable. Unless you are building an app on an existent, I would also remove name and #Table.
One of my classes that convert table in SQL Server database to entities.
#Id
#Column(name = "[primary_key]")
private int id;
#Column(name = "[id Employee]")
private byte[] employeeId;
#Column(name = "[Period]")
private Date period;
#Column(name = "[Credit amount]")
private float creditAmount;
#Column(name = "[Type]")
private String type;
#Column(name = "[Weight 585]")
private float weight585;
and CrudRepo for it (partial):
#Query(value = "select sum([Weight 585]) from [ZOK per period] where ([Period] between ?1 and ?2) " +
"and ([id Employee] = ?3) and ([Type] = 'Gold')", nativeQuery = true)
Double getSumZokGold(Date start, Date end, byte[] employeeId);
All my column names are red and underlined with two errors:
Cannot resolve column '';
Unknown database function '';
I checked persistence and data already assigned - its most popular solution of this problem. I checked dialect - SQL Server, already. I checked ALL column names and didn't see any mistake.
To solve this problem you may do:
Check all columns names - yes, problem can be just in it. By the way, you will have an Error if its true.
Add new Database in Database (right IDE side) and connect to your data source.
Check your Persistence (bottom left corner in "Intellij IDEa"), press Assign Data... and use in Data Source not default source, but exactly yours.
If you have already connected database (MS Server, for example) - reconnect. Yes, its useful too.
Now, if your problem still annoying you - open File - Invalidate Caches / Restart and choose blue button Invalidate and Restart.
P.S.: If you use russian names for your table columns in database - write them as #Column(name = "[...]") and in SQL #Query put N before value in "" or write name in brackets. But the best idea - use just English!
Query:
INSERT INTO PERSON
(email, mobile, party_id, affiliate_id, eligibility, member_start_date, created_by, created_dt, first_name, last_name, google_connected)
values
('xxx#yyy.org', NULL, 123, '123', '1', NULL, NULL, '2018-8-30 21:45:56.859000 -6:0:0', 'xxx', 'yyy', '0')
ON CONFLICT (email)
DO UPDATE SET create_dt = '2018-8-30 21:45:56.859000 -6:0:0' where email = ?
When the LocalDate value is not null, it works fine. Facing this issue only when LocalDate value is given as null.
Even after PostgreSQL casting, it does the same.
Exception stacktrace:
2018-08-30 21:10:48,372 -- [ERROR]-- There was an unexpected problem
with your request org.postgresql.util.PSQLException: ERROR: column
"member_start_date" is of type date but expression is of type bytea
Hint: You will need to rewrite or cast the expression. Position: 185
at
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2182)
at
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1911)
at
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:173)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:645)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:495)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:380) at sun.reflect.GeneratedMethodAccessor98.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) at
org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
at com.sun.proxy.$Proxy185.executeQuery(Unknown Source) at at
org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
... 149 common frames omitted
Entity:
#Entity(name = "person")
#EqualsAndHashCode(callSuper = false)
public class PersonEntity extends Audit {
#Id
#GeneratedValue
#Column(name = "person_id", columnDefinition = "uuid", updatable = false)
private UUID id;
#Column(name = "first_name")
private String firstName;
#Column(name = "last_name")
private String lastName;
#Column(name = "email")
#NotNull
private String email;
#Column(name = "mobile")
private String mobile;
#Column(name = "party_id")
private Long partyId;
#Column(name = "affiliate_id")
private String affiliateId;
#Column(name = "eligibility")
#NotNull
private Boolean eligibility;
#Column(name = "member_start_date")
private LocalDate memberStartDate;
#Column(name = "google_connected")
private Boolean googleAccountConnected;
}
PostgreSQL table definition; it's missing google_connected column which is not important:
CREATE TABLE person
(
person_id UUID NOT NULL,
email VARCHAR(128) NOT NULL,
mobile VARCHAR(20),
party_id INTEGER,
affiliate_id VARCHAR(20),
eligibility BOOLEAN NOT NULL,
member_start_date DATE,
created_by VARCHAR(128) NOT NULL,
created_dt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_by VARCHAR(128) DEFAULT NULL,
updated_dt TIMESTAMP NULL,
CONSTRAINT person_pk PRIMARY KEY ( person_id )
);
Because the query is native, Hibernate doesn't know the data types which to expect, so when you pass a null it defaults to the generic Serializable type handler. Changing this behaviour breaks compatibility with other databases.
Postgres, however parses the query immediately and determines what types are acceptable, and it always checks for type before it checks for null. They are the only ones who can fix this, but refuse to do so and say it works as intended.
The only solutions for you are:
use JPQL
use managed entities
use hard-coded nulls in the query string where you need to
Fortunately for the third option, with Hibernate you can use named parameters in native queries, so you don't have to do positional calculations for when something is available and when it isn't.
edit: 4th solution that I've discovered since.
You have your query:
Query q = em.createNativeQuery("UPDATE...");
Have some static final LocalDate object somewhere:
public static final LocalDate EPOCH_DATE = LocalDate.of(1970, 1, 1);
then call the query like this:
q.setParameter("start_date", EPOCH_DATE);
q.setParameter("start_date", nullableDateParam);
The first time you call setParameter for a parameter, Hibernate uses the class to resolve the type. The second time you call it, the type is already resolved, so a null will work.
It's an old question, but there is a more useful way:
your query...
.setParameter("transaction_id", null, LongType.INSTANCE)
It works.
Found from https://forum.hibernate.org/viewtopic.php?p=2493645
Going to newer versions of hibernate 5.1.17 and above + postgres seems to have exhibited this behavior. Looking into the code, when it binds a type that has no value, the old hibernate code attempted to resolve the type through a typeresolver. The newer versions of hibernate's implementation state that it will not guess.
public Type resolveParameterBindType(Object bindValue) {
if ( bindValue == null ) {
// we can't guess
return null;
}
We ended up just setting a default value based on the type first, and then the real null value.