简体   繁体   中英

java.lang.NoSuchMethodException: com…Employee.<init>()

I have this class for which I have a constructor:

@Entity
data class Employee(
    @field: Id
    @field:GeneratedValue var id : Long = 0,
    var username : String = "",
    var password : String ="",
    var name : String ="",
    var lastName: String ="",
    var phone : String="",
    var email : String ="",
    var sex : String ="",
    var active : Boolean = false,
    @field: ManyToOne(targetEntity = District::class)
    var district : District?,
    @field: ManyToOne(targetEntity = Company::class)
    var company : Company?,
    var picture:String="",
    var resetPasswordCode:String="",
    // código que se le envía al usuario en el momento que crea
    // la cuenta, deberá abrir en enlace del correo para activarla
    // de ahí que "active" sea false.
    var activationCode : String="",
    var enabled : Boolean = true,
    var accountNonExpired: Boolean = true,
    var credentialsNonExpired: Boolean = true,
    var accountNonLocked : Boolean = true,
    @field: OneToMany(targetEntity = Roles::class) var roles :MutableSet<Roles> = mutableSetOf())

{
    fun toUser() : User
    {
        val authorities = mutableSetOf<GrantedAuthority>()
        roles.forEach{authorities.add(SimpleGrantedAuthority(it.role))}
        return User(username,password,enabled,accountNonExpired,credentialsNonExpired,accountNonLocked, authorities)
    }
}

I have a controller method and for testing, I just print the "saving" text (as I'm having this issue, it doesn't make sense (to me) to go any further):

@RequestMapping(method = arrayOf(RequestMethod.POST))
fun doPost(employee: Employee) : String {
    println("saving!!!")
    // employeeService.save(employee)
    return "redirect:/display"
}

When sending the form, I get this error:

java.lang.NoSuchMethodException: com.almasoft.facturapp.entities.Employee.<init>()

The form is this:

<form th:action="@{/register}" method="post" role="form" 
    th:object="${employee}">
    <legend>Register a User</legend>

    <div class="form-group">
        <label for="username">User Name</label>
        <input type="text" class="form-control" name="username" id="username" th:field="*{username}"></input>
    </div>

    <div class="form-group">
        <label for="password">Password</label>
        <input type="password" class="form-control" name="password" id="password" th:field="*{password}"></input>
    </div>

    <button type="submit" class="btn btn-primary">Submit</button>
</form>

I'm following this post and adapting the code for my needs, but I can't get this to work :(

So, why would it complain about a default constructor if there is already one?

And: how can I solve this?

@field: ManyToOne(targetEntity = District::class)
var district : District?,
@field: ManyToOne(targetEntity = Company::class)
var company : Company?,

These don't have default values so empty constructor is not generated.

Add = null to each.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM