Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8989033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:09:13+00:00 2026-06-15T22:09:13+00:00

I have a User object mapped with hibernate annotations working just fine. Eg @Entity

  • 0

I have a User object mapped with hibernate annotations working just fine.

Eg

@Entity
public class User implements Serializable {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long userId;

   etc ..
}

I then have a “Add User” form using Spring 3 MVC and I required a command object to back this form, so I subclassed User from above in UserCommand. The UserCommand has some extra stuff on it related to the web interface etc that doesn’t need to be saved as part of a User entity.

Eg

public class UserCommand extends User {

    private String initialAddress;

    etc
}

So my view/presentation layer basically creates a UserCommand object, fills out the user details and then the controller submits this down to the service/dao layer to persist. Since UserCommand extends (“is-a”) User the dao accepts the UserCommand instance and it passes the dao validation checks (eg ensure has username and password filled out).

However when hibernate actually comes to persist the object as an entity in the db, it seems to realise the actual object is a UserCommand, which isn’t a mapped entity, even though it’s super type is.

The resulting error is;

   org.hibernate.MappingException: Unknown entity: com.example.UserCommand
       org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:691)
       org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1494)
       org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:202)
       org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:531)
       org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:102)
       org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
       org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:808)
       org.hibernate.impl.SessionImpl.persist(SessionImpl.java:782)
       org.hibernate.impl.SessionImpl.persist(SessionImpl.java:786)

Is there a way around this issue? I seemed like an obvious thing to do, the form is filling out a User so just use an extension of User (ie UserCommand) as the form’s backing command object.

Or am I going to have to break the inheritance, duplicate User’s fields in UserCommand and the explicitly copy all the values from UserCommand into User during the form submit?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:09:15+00:00Added an answer on June 15, 2026 at 10:09 pm

    I think mixing database and view entities is a bad practice. These are different layers and they should operate on different DTOs. Usually I use following pattern to convert between two layers:

    public class UserCommand {
        public static UserCommand fromUser(User user) {
            UserCommand command = new UserCommand();
            // fill UserCommand fields
            return command;
        }
    
        public void toUser(User user) {
            // fill User fields
        }
    }
    

    If conversion between view and database DTOs require some complex logic you can move these methods to conversion service.

    One benefit from splitting these entities is validation. You can specify JSR-303 annotations to validate these beans and in most cases they will be different for UI and DB.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user object like so: public class User { public Guid UserId
I have a simple POJO mapped to a table using Hibernate. Which works just
I have a class mapped as an Entity to persist it in a database.
I have a class like public User class { public string Name{get;set;} public string
Following scenario: I have user object: ... @Column(name = TITLE) private String title; @NotNull
I want to integrate chargify to my rails app. I have user object and
I have an user object for my restful service, which has an userid and
Let's say I have an object - a User object in this case -
I have a data model object User . My app also has some other
I have a script which creates a user-defined object like this: obj = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.