Hibernate – I am getting the following message in the Console: Hibernate:alter table UserDetails_listofAddress drop constraint FK_a254xtntunnm64c0vo7oha0ol

hibernate

I am unable to persist the set of address into the data base and I am getting the following exceptions . like Hibernate:

alter table UserDetails_listofAddress 
    drop constraint FK_a254xtntunnm64c0vo7oha0ol.  

I am getting the following exception also INFO:

HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException.  

I am providing the hibernate configuration file , Value object class , class that is supposed to be persisted and also the test class .

Hibernate Configuration file

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">708477</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
        <property name="connection.pool_size">10</property>


        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <mapping class="com.sanjay.hibernateDTO.UserDetails"/>
    </session-factory>

</hibernate-configuration>

UserDetails.java
@Entity
@Table(name = "USER_DETAILSS")
public class UserDetails {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private int userId;
private String userName;
@Embedded
private Address physicaladdress;

    @ElementCollection
    private Set<Address> listofAddress = new HashSet();

    public Set<Address> getListofAddress() {
        return listofAddress;
    }
    public void setListofAddress(Set<Address> listofAddress) {
        this.listofAddress = listofAddress;
    }
    public Address getOfficeAddresss() {
        return officeAddresss;
    }

    public void setOfficeAddresss(Address officeAddresss) {
        this.officeAddresss = officeAddresss;
    }

    public Address getPhysicaladdress() {
        return physicaladdress;
    }

    public void setPhysicaladdress(Address physicaladdress) {
        this.physicaladdress = physicaladdress;
    }


    @Embedded
    @AttributeOverrides

    ({
    @AttributeOverride(name = "street", column = @Column(name = "HOME_STREET_NAME")),
    @AttributeOverride(name = "state", column = @Column(name = "HOME_STATE_NAME")),
    @AttributeOverride(name = "city", column = @Column(name = "HOME_CITY_NAME")),
    @AttributeOverride(name = "pincode", column = @Column(name = "HOME_ZIPCODE"))

    })

    private Address officeAddresss;
    @Temporal(TemporalType.TIME)
    private Date joinedDate;

    public Date getJoinedDate() {
        return joinedDate;
    }

    public void setJoinedDate(Date joinedDate) {
        this.joinedDate = joinedDate;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    private String address;         

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }       
}

Address Class

@Embeddable
public class Address {
    @Column(name="USER_STREET")
    private String street;

    @Column(name="USER_CITY")
    private String city;

@Column(name="USER_STATE")
private String state;

@Column(name="USER_PINCODE")
private String pincode;

public String getStreet() {
    return street;
    }

public void setStreet(String street) {
    this.street = street;
    }

public String getCity() {
    return city;
    }

public void setCity(String city) {
    this.city = city;
    }

public String getState() {
    return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getPincode() {
        return pincode;
    }

    public void setPincode(String pincode) {
        this.pincode = pincode;
    }

}

HibernateTest.java

public class HibernateTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        UserDetails user = new UserDetails();


        user.setUserName("FirstUser");
        user.setJoinedDate(new Date());
        user.setAddress("ADDRESS JOINED");

        Address addr = new Address();
        addr.setCity("Hyderabad");
        addr.setPincode("50001");
        addr.setState("Telangana");
        addr.setStreet("Banjara Hills");
        user.getListofAddress().add(addr);

        Address homeaddress=new Address();
        homeaddress.setCity("NEW CITY");
        homeaddress.setPincode("55551");
        homeaddress.setState("NEW STATE");
        homeaddress.setStreet("NEW STREET");
        user.getListofAddress().add(homeaddress);

        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session session = sf.openSession();
        session.beginTransaction();

        session.save(user);
        session.getTransaction().commit();
        session.close();
        user = null;

        session = sf.openSession();
        session.beginTransaction();
        user = (UserDetails) session.get(UserDetails.class, 1);
        System.out.println(user.getUserName());
    }
}

OUTPUT printed in the console

May 26, 2014 3:56:39 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
May 26, 2014 3:56:39 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.5.Final}
May 26, 2014 3:56:39 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.max_fetch_depth=5, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE, hibernate.bytecode.use_reflection_optimizer=false, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=5}
May 26, 2014 3:56:39 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
May 26, 2014 3:56:39 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
May 26, 2014 3:56:39 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
May 26, 2014 3:56:39 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
May 26, 2014 3:56:39 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
May 26, 2014 3:56:39 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
May 26, 2014 3:56:39 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/hibernatedb]
May 26, 2014 3:56:39 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=postgres, password=****}
May 26, 2014 3:56:39 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
May 26, 2014 3:56:39 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 10 (min=1)
May 26, 2014 3:56:40 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
May 26, 2014 3:56:40 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
May 26, 2014 3:56:40 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
May 26, 2014 3:56:40 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
May 26, 2014 3:56:41 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Hibernate: 
    alter table UserDetails_listofAddress 
        drop constraint FK_a254xtntunnm64c0vo7oha0ol

Best Answer

These logged messages have nothing to do with your issue.

alter table UserDetails_listofAddress 
drop constraint FK_a254xtntunnm64c0vo7oha0ol.

The message above is telling you that Hibernate is recreating your schema:

HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException.

The message above is because your driver doesn't support JDBC4 lob creation

The real problem is here:

session = sf.openSession();
session.beginTransaction();
user = (UserDetails) session.get(UserDetails.class, 1);
System.out.println(user.getUserName());

You simply forgot to end the currently running transaction: commit or rollback.

So, change it to:

Session session = openSession();
session.beginTransaction();

user = (UserDetails) session.get(UserDetails.class, 1);
System.out.println(user.getUserName());

session.getTransaction().commit();
session.close();

Although this will fix your issue, you are far from being done. You still need to:

  1. Use a logging framework instead of System.out.println
  2. Use try/finally blocks to end transactions and close the Session.
Related Topic