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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:11:12+00:00 2026-05-24T03:11:12+00:00

I have similar problem as is described in Spring Framework – New object is

  • 0

I have similar problem as is described in Spring Framework – New object is created in DB instead of updating but I have two entities: Author and Department in @ManyToOne relation. One department has many authors and one author belongs to one department. When I update an author, author details are updated, but department is inserted which leads to duplicate entries. From aforementioned link I know that I’m probably missing department ID, but I don’t know how to tie department with its ID.

Author entity (without getters and setters)

@Entity
@Table(name = "author")
public class Author implements Serializable {

    @Id
    @Column(name = "idauthor")
    @GeneratedValue
    private Integer idAuthor;

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

    @ManyToOne(cascade = CascadeType.ALL)
    private Department department;
}

Department entity (without getters and setters)

@Entity
@Table(name="department")
public class Department implements Serializable {

    @Id
    @Column(name="iddepartment")
    @GeneratedValue
    private Integer iddepartment;

    @Column(name="name", unique=true)
    private String name;
}

Methods for update operatio of Author in AuthorController

@Controller
@RequestMapping("/author")
public class AuthorController {

    @Autowired
    private AuthorService authorService;

    @Autowired
    private DepartmentService departmentService;

    @RequestMapping(value="/save/{authorId}", method = RequestMethod.GET)
    public String renderUpdateForm(@PathVariable("authorId") Integer authorId, ModelMap model) {
        model.addAttribute("author", authorService.getAuthorByID(authorId));

        model.addAttribute("departmentList", departmentService.listDepartment());
        return "author.save";
    }

    @RequestMapping(value="/save/{authorId}", method = RequestMethod.POST)
    public String updateAuthor(@ModelAttribute("idAuthor") Author author, BindingResult result, SessionStatus status) {
        authorService.updateAuthor(author);
        status.setComplete();
        return "redirect:/author";
    }
}

authorService.updateAuthor(author); calls service which calls DAO class with method:

@Repository
@Transactional
public class AuthorDAOImpl implements AuthorDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void updateAuthor(Author author) {
        sessionFactory.getCurrentSession().update(author);
    }
}

and finally JSP with form

<form:form method="post" action="${pageContext.request.contextPath}/author/save" modelAttribute="author">
<form:hidden path="idAuthor" />
    <table>
    <tr>
        <td>
            <form:label path="name">
                <spring:message code="label.author.name"/>
            </form:label>
        </td>
        <td><form:input path="name" /></td>
    </tr>
    <tr>
        <td>
            <form:label path="department.name">
                <spring:message code="label.author.department"/>
            </form:label>
        </td>
        <td>
            <form:select path="department.name">
                <form:options items="${departmentList}" itemValue="name" itemLabel="name" />
            </form:select>
        </td>        
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="<spring:message code="label.update" />
        </td>
    </tr>
</table>
</form:form>

And the error I’m getting is

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Duplicate entry ‘custom department
name’ for key ‘name’

How should I pass ID to department or fix my problem? Thanks for your advises.

  • 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-05-24T03:11:12+00:00Added an answer on May 24, 2026 at 3:11 am

    I had to add Formatters SPI (see documentation for more information) for each POJO. Then hibernate updated objects instead of saving duplicity.

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

Sidebar

Related Questions

I have similar problem to one discussed here , but with stronger practical usage.
I suppose similar problem would have been discussed here, but I couldn't find it.
I have a problem similar to the one described here: MVC T4 MvcTextTemplateHost and
I have a problem which seems very similar to the one described in http://markmail.org/message/6rlrzkgyx3pspmnf
I have encountered a similar problem described here (and in other places) - where
I've got a problem similar to,but subtly different from, that described here (Loading assemblies
I have a problem similar to the one described in the link below. NSHTTPURLResponse
I have problem similar to describe here , but a bit more complicated. There
I have a very similar problem as described here . I also upgraded a
I have a problem similar to the one described by LutherBaker here , i.e.

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.