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 8312955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:19:57+00:00 2026-06-08T20:19:57+00:00

Hi I am trying to do one to many insert but I am having

  • 0

Hi I am trying to do one to many insert but I am having problems.
I have two tables:

CREATE TABLE users_app (
  user_id int UNSIGNED NOT NULL AUTO_INCREMENT,
  user_number varchar(45) NOT NULL default '0',
  user_password varchar(45) NOT NULL default '0',
  os int(1) unsigned NOT NULL,
  token varchar(500) NOT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;

CREATE TABLE user_app_devices(  
    id int AUTO_INCREMENT PRIMARY KEY,  
    user_id int UNSIGNED NOT NULL,
    device_name varchar(45) NOT NULL,
    FOREIGN KEY (user_id) REFERENCES users_app (user_id)  
)ENGINE=InnoDB CHARSET=utf8;

My classes:

@Entity
@Table(name="user_app_devices")
public class UserAppDevice implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private int id;

@Column(name="device_name")
private String deviceName;

//bi-directional many-to-one association to UsersApp
@ManyToOne
@JoinColumn(name="user_id")
private UsersApp usersApp;

public UserAppDevice() {
}

public int getId() {
    return this.id;
}

public void setId(int id) {
    this.id = id;
}

public String getDeviceName() {
    return this.deviceName;
}

public void setDeviceName(String deviceName) {
    this.deviceName = deviceName;
}

public UsersApp getUsersApp() {
    return this.usersApp;
}

public void setUsersApp(UsersApp usersApp) {
    this.usersApp = usersApp;
}

}

@Entity
@Table(name="users_app")
public class UsersApp implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name="user_id")
private int userId;

private int os;

private String token;

@Column(name="user_number")
private String userNumber;

@Column(name="user_password")
private String userPassword;

//bi-directional many-to-one association to UserAppDevice
@OneToMany(mappedBy="usersApp")
private List<UserAppDevice> userAppDevices;

public UsersApp() {
}

public int getUserId() {
    return this.userId;
}

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

public int getOs() {
    return this.os;
}

public void setOs(int os) {
    this.os = os;
}

public String getToken() {
    return this.token;
}

public void setToken(String token) {
    this.token = token;
}

public String getUserNumber() {
    return this.userNumber;
}

public void setUserNumber(String userNumber) {
    this.userNumber = userNumber;
}

public String getUserPassword() {
    return this.userPassword;
}

public void setUserPassword(String userPassword) {
    this.userPassword = userPassword;
}

public List<UserAppDevice> getUserAppDevices() {
    return this.userAppDevices;
}

public void setUserAppDevices(List<UserAppDevice> userAppDevices) {
    this.userAppDevices = userAppDevices;
}

public UsersApp(int os, String token, String userNumber, String userPassword) {
    this.os = os;
    this.token = token;
    this.userNumber = userNumber;
    this.userPassword = userPassword;
}

I want to add new user with device

I try this code:

    Session session = (Session) em.getDelegate();
    session.beginTransaction();

    UsersApp user = new UsersApp(os, token, userNumber, userPassword);

    session.save(user);

    UserAppDevice ud = new UserAppDevice();

    ud.setUsersApp(user);
    ud.setDeviceName(device);

    session.save(ud);

    session.getTransaction().commit();

but I am facing exception:

13:16:48,516 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http--0.0.0.0-8080-3) SQL Error: 1452, SQLState: 23000
13:16:48,517 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http--0.0.0.0-8080-3) Cannot add or update a child row: a foreign key constraint fails (`application`.`user_a
pp_devices`, CONSTRAINT `user_app_devices_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users_app` (`user_id`))
13:16:48,520 ERROR [org.jboss.as.ejb3.tx.CMTTxInterceptor] (http--0.0.0.0-8080-3) javax.ejb.EJBTransactionRolledbackException: Cannot add or update a child row: a foreign key const
raint fails (`application`.`user_app_devices`, CONSTRAINT `user_app_devices_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users_app` (`user_id`))
13:16:48,524 ERROR [org.jboss.ejb3.invocation] (http--0.0.0.0-8080-3) JBAS014134: EJB Invocation failed on component DeviceRegisterDAOImpl for method public abstract void com.break
id.ejb.model.DeviceRegisterDAO.add(int,java.lang.String,java.lang.String,java.lang.String,java.lang.String): javax.ejb.EJBTransactionRolledbackException: Cannot add or update a chi
ld row: a foreign key constraint fails (`application`.`user_app_devices`, CONSTRAINT `user_app_devices_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users_app` (`user_id`))
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleInCallerTx(CMTTxInterceptor.java:139) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.java:204) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:306) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:190) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]
        at org.jboss.as.ejb3.remote.EJBRemoteTransactionPropagatingInterceptor.processInvocation(EJBRemoteTransactionPropagatingInterceptor.java:80) [jboss-as-ejb3-7.1.1.Final.jar:
7.1.1.Final]

What am I missing ?

  • 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-08T20:19:59+00:00Added an answer on June 8, 2026 at 8:19 pm

    You haven’t told Hibernate that the ID of UserApp was generated automatically by the database:

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name="user_id")
    private int userId;
    

    (and do the same for the other entity)

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

Sidebar

Related Questions

i am trying to persist two entities(with one to many relationship) using jpa but
I am trying to create one to many relation using one table. Is this
I have two tables, Table_A and Table_B. Table_A has a one-to-many relationship with Table_B.
I am trying to define a one-to-many relationship between my two table in Entity
I am trying to implement a one-to-many relationship using NHibernate 2.1.2 but keep getting
I am trying to do something like this, but using @One-To-Many instead of @ElementCollection.
I have 2 tables (Item and Code) with relation one to many(one Item can
I have small problem when trying to create many-to-many link in doctrine. I have
I am trying to copy values directly from one mysql table to another... but
I am trying to learn one to many Mapping in Hibernate . The below

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.