Hibernate Tutorial 13 - One To One Mapping

Share
Embed
  • Published on May 26, 2011
  • One to one mapping of entities.

Comments • 36

  • Priyobrato Das
    Priyobrato Das Year ago

    Exception in thread "main" org.hibernate.MappingException: Could not determine type for: io.priyo.hibernate.Vehicle, at table: USERREG, for columns: [org.hibernate.mapping.Column(vehicle)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:456)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:423)
    at org.hibernate.mapping.Property.isValid(Property.java:226)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:597)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:265)
    at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:459)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:710)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
    at io.priyo.hibernate.execution.MainClass.main(MainClass.java:43)
    Any idea where I am lagging?

    • thisisnimish4 thisisnimish4
      thisisnimish4 thisisnimish4 4 months ago

      Yeah, in my case there was no such database created as the vehicleId which was generated was of type String. When I changed it to int, the proper Table was created and then I was able to insert the entity.

    • thisisnimish4 thisisnimish4
      thisisnimish4 thisisnimish4 4 months ago

      I am getting the same issue. Let me know whether you were able to solve it

  • Vishwambhar Bhat
    Vishwambhar Bhat Year ago

    How to get the joined table data?

  • Julio Jaimes Rodriguez

    if you have this error : The internal connection pool has reached its maximum size and no connection is currently available!
    you can solve it changing:

    1
    to:

    100
    in file hibernate.cfg.xml.

  • Devi Prasad Patnaik

    Normal Hibernate xml file mapping is using Configuration()
    new Configuration().configure().buildSessionFactory();
    But for Annotation use the below line of code
    new AnnotationConfiguration().configure().buildSessionFactory();

  • Varun Kumar
    Varun Kumar Year ago

    I got below error:
    Caused by: org.postgresql.util.PSQLException: ERROR: column "vehicle_id" of relation "userdetails" does not exist
    Position: 36
    --------------------
    Later found the reason being:
    I missed
    @Table (name="User_details")
    In user details table.
    docs.oracle.com/javaee/5/api/javax/persistence/Table.html
    This annotation specifies the primary table for the annotated entity. Additional tables may be specified using SecondaryTable or SecondaryTables annotation.

  • Rohan Sharma
    Rohan Sharma 2 years ago +2

    Please use @OneToOne(cascade = {CascadeType.ALL})
    above declaring the Vehicle in Users class.

  • SuperNunien
    SuperNunien 3 years ago +2

    Is the session.save(vehicle) is stille needed after you added @OneToOne ?

  • Марко Петковић

    If we try this in HibernateTest class:
    UserDetails user = new UserDetails();
    user.setUserName("First User");
    UserDetails user2 = new UserDetails();
    user2.setUserName("Second User");

    Vehicle vehicle = new Vehicle();
    vehicle.setVehicleName("Car");

    user.setVehicle(vehicle);
    user2.setVehicle(vehicle);
    It will add both users in the table User_Details and both of them will have VEHICLE_ID = 1. But it shouldn't be possible in One-To-One relationship: Only one user can be connected with only one vehicle, and only one vehicle can be connected with only one user. Right?

  • Kesav Nallan
    Kesav Nallan 3 years ago +3

    Wrong Example for One - One.... The example looks like one-to-many, one vehicle can be assigned to multiple users. i.e user1.setVehicle(vehicle) & user2.setVehicle(vehicle).... I tried to create one-one relation with user_detail reference in vehicle, ........................ & vehicle reference in User_details table, that makes a circular reference issue. finding correct example....

  • Arun Vajpayee
    Arun Vajpayee 3 years ago

    hey ! all of your videos are awesome...very enlightening......big thanks :)

  • Shivangi Gupta
    Shivangi Gupta 3 years ago +1

    You can use ctrl+Shift+O for importing all the respective classes.

  • Roberto M.
    Roberto M. 3 years ago +1

    I have a Question: Don't you need @OneToOne in Vehicle entity too? If I am looking at it the correct way, ifa user puts a repeated vehicle ID into User_details table it will become a "Many To One" Relationship. Is it the other way around?

  • horay hophop
    horay hophop 4 years ago +9

    if anyone gets the error which is " Exception in thread "main" org.hibernate.exception.SQLGrammarException: Unknown column 'vehicle_vehicleId' in 'field list' " , the solution is you should change property name="hbm2ddl.auto" from create to update .

  • Tarun Sharma
    Tarun Sharma 4 years ago +1

    Some options to try:
    Specify the @Table annotation and give it a name of Vehicle OR
    Change the hbm2ddl.auto to "update"

  • Wen Hou
    Wen Hou 4 years ago

    if you have been following the tutorial, you have other tables depend on user_details, so when hibernate tries to delete user_details, it cannot, but it doesn't report the error either. so you ended up with an old user_details table with no vehicle_id column. Try to manually drop all your tables, try to drop user_details table first and you will see what I am talking about.

  • Santanu Bhattacharyya
    Santanu Bhattacharyya 4 years ago

    In one-to-one mapping, if I delete a user row, corresponding column for car is also getting deleted which should not be. I want to keep the entity even if the user gets deleted. How to prevent it?
    This is actually creates two problems:
    a. Say you have persisted a vehicle. I have persisted a user with that vehicle. If I delete my user, vehicle you persisted should not be deleted.
    b. Say I have persisted two users with a same vehicle (vehicle ID is same). Now if I want to delete one user, Hibernate will also try to remove the corresponding vehicle. But wont be able to do so because its also being used by another user. Foreign key would prevent the vehicle's deletion. As a result I wont be able to delete the user I wanted to.
    Does anyone have a solution to this?

  • Tuhin Bagh
    Tuhin Bagh 4 years ago

    How to retrieve data and iterate using join?

  • Sagittarius555555555
    Sagittarius555555555 5 years ago

    the code did not execute until I made Vehicle class serializable. but after that I got serial number of car in Users vehicle column.

  • Sagittarius555555555
    Sagittarius555555555 5 years ago +2

    I get following ERROR: "Could not determine type for: com.mydomain.hibernate.dto.Vehicle, at table: USER_DETAILS, for columns: [org.hibernate.mapping.Column(vehicle)]"

  • Tanvirul Islam
    Tanvirul Islam 5 years ago +2

    Hello Koushik, in the example here, there is a possibility that one car can be owned by many user since, vehicle_id is FK (and not unique) in the user_detail table.

  • Jayanth Babu
    Jayanth Babu 5 years ago +3

    Hi,
    How to create 2 separate sequence ID, both the user id and the vehicle id gets their next val using the same sequence... ?

  • Sushil
    Sushil 5 years ago

    Why the VEHICLE class has no "@TABLE" annotation?

  • Srini
    Srini 6 years ago

    Thanks alot for all your videos for Hibernate and Spring. It helped me alot.

  • Ramiro Jiménez
    Ramiro Jiménez 6 years ago

    Hi your videos are great, but i have a question:
    When i do the Reverse Engineer in MySQL (Workbench), the diagram shows me a relationship OnetoMany; why?
    I really do all the things in the video :D

  • MsWhoy
    MsWhoy 6 years ago

    dropall tables manually

  • Konrad Wudkowski
    Konrad Wudkowski 6 years ago +1

    many people seem to have problems, consider dropping all tables manually, and use following annotation @OneToOne(cascade=CascadeType.ALL) instead of just @OneToOne

  • Sandeep M
    Sandeep M 7 years ago

    @oneToOne:generated query is wrong&wrong records:
    Hibernate: insert into Students (city,state, zipcode,First_name, Last_name) values (?, ?, ?, ?, ?)
    Hibernate: insert into Students (city, state, zipcode, First_name, Last_name) values (?, ?, ?, ?, ?)
    Hibernate: insert into Teachers (city, state, zipcode, First_name, Last_name, students_studentsId) values (?, ?, ?, ?, ?, ?)
    Hibernate: insert into Teachers (city, state, zipcode, First_name, Last_name, students_studentsId) values (?, ?, ?, ?, ?, ?)

  • Sandeep M
    Sandeep M 7 years ago

    Please find my last comment and pls help me in fixing the problem.
    Thank you. if you want I can send my code.

  • Sandeep M
    Sandeep M 7 years ago

    @Entity
    public class Teachers implements Serializable {
    @OneToOne
    private Students students;
    }
    @Entity
    public class Students implements Serializable {}
    Hibernate: insert into Students (city, state, zipcode, First_name, Last_name) values (?, ?, ?, ?, ?)
    Hibernate: insert into Teachers (city, state, zipcode, First_name, Last_name, students) values (?, ?, ?, ?, ?, ?)
    ....DataException: could not insert:[Teachers]
    Caused by:MysqlDataTruncation:Data too long for column 'students' at row 1

  • NutritiveSoul
    NutritiveSoul 7 years ago

    let's see what happens if you run this
    =P

  • O2profs
    O2profs 7 years ago

    i have same problem also create property in the xml configuration file appear to be not working it act like upgrade

  • Raju Gurung
    Raju Gurung 7 years ago

    So far I have been following everything you've done and successfully did it myself as well. But the code for this video gives me the following error? Can you please help?
    Exception in thread "main" org.hibernate.exception.SQLGrammarException: Unknown column 'vehicle_vehicleId' in 'field list'