简体   繁体   中英

Hibernate doesn't persist when inserting long texts

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,

  1. You can set columnDefinition like

     @Column(name = "notes", columnDefinition = "varchar(300)") private String notes; 
  2. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM